How to Pass a JavaScript Variable to Another Browser Window

Can I pass a JavaScript variable to another browser window?

Provided the windows are from the same security domain, and you have a reference to the other window, yes.

Javascript's open() method returns a reference to the window created (or existing window if it reuses an existing one). Each window created in such a way gets a property applied to it "window.opener" pointing to the window which created it.

Either can then use the DOM (security depending) to access properties of the other one, or its documents,frames etc.

How to pass a variable from current window to another window

You can try localStorage for this.

Add item which you want to pass on another page:

localStorage.setItem("key", "value");

On another page you can access through:

localStorage.getItem("key");

You can store object data as well in localStorage. Note that clear localStorage if you're storing some sensitive information.

And simpler option is to use query parameter, but I think you're not okay with it.

Or you can use sessionStorage:

sessionStorage.setItem("key", data);
window.open("page_url","_blank");

on another page use:

$(document).ready(function(){        
var data = sessionStorage.getItem("key");
console.dir(data);
});

Note: The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.

How to pass variable from one browser window to the another browser window (Same browser) in IE 11?

You can easily do this with window.open property,

For example,

    printDocument() {
//generate render link
let printURL = this.apiBaseUrl + "WestCore/GuaranteeLetter/" + this.selectedDocumentId + "/RenderGuaranteeLetter?reportPath=" + encodeURIComponent(this.selectedTemplate);
window.open(printURL);
}

Or this is how i render blob images in my project using window.open...

  createImageFromBlob(image: Blob) {
if (window.navigator.msSaveOrOpenBlob) // FOR IE10+
window.navigator.msSaveOrOpenBlob(image, "download." + (image.type.substr(image.type.lastIndexOf('/') + 1)));
else {
var url = window.URL.createObjectURL(image);
window.open(url);
}
}

Passing Variable through JavaScript from one html page to another page

Without reading your code but just your scenario, I would solve by using localStorage.
Here's an example, I'll use prompt() for short.

On page1:

window.onload = function() {
var getInput = prompt("Hey type something here: ");
localStorage.setItem("storageName",getInput);
}

On page2:

window.onload = alert(localStorage.getItem("storageName"));

You can also use cookies but localStorage allows much more spaces, and they aren't sent back to servers when you request pages.

How to pass variables to new window using Javascript and postMessage

Use localStorage like this:

function buttonClick() {
localStorage.setItem("mySepcialSetting", "Hello World");
window.location.href = "FindApark.html";
}

Then you can access it as follows:

var mySpecialSetting = localStorage.getItem("mySepcialSetting");
console.log(mySpcialSetting);

How to pass a value from a parent window to another html page using javascript?

I hope i need to elaborate the below code

Button on click function in the home page:

function sample(){
//this will set the text box id to var id;
var id = document.getElementById("text_box_id").id;

//the sessionStorage.setItem(); is the predefined function in javascript
//which will support for every browser that will store the sessions.
sessionStorage.setItem("sent", id);

//this is to open a window in new tab
window.open("result.html","_blank");
}

Retrieve the value in result page:

$(document).ready(function(){
//This sessionStorage.getItem(); is also a predefined function in javascript
//will retrieve session and get the value;
var a = sessionStorage.getItem("sent");
alert(a);
});

For more information about sessionStorage

  • code.google.com/p/sessionstorage/
  • developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

I have done same thing as above, am getting values in new window that's great, but that values I am getting only in documet.ready() function. So I am not able to use these values in my JSP. once I got values I need to display them in JSP.

How to pass variable from one Javascript to another Javascript file

see about local and global variables for more info. http://www.w3schools.com/js/js_scope.asp.

Make sure your var X is not inside a function and that your file is load in the correct order.

<script src="file1.js"><script> //declare var x=1 here
<script src="file2.js"><script> // you can access x from here.

Pass javascript string variable to another page and get the variable in PHP with AJAX

Capturing as an answer for future readers...

Fundamentally what's happening here is that two requests are being made to the target page. The first one is the AJAX request:

$.ajax({
type: "POST",
url: 'example.com/artiste/mise-en-relation/',
data: { savedArtists : savedartists },
success: function(data) {
//...
}
});

This is a POST request which contains the data you expect, and works just fine. However, the result of this request is being ignored. That result is available in the success callback, but the code doesn't do anything with it:

console.log("success!");
location.href = "example.com/artiste/mise-en-relation/";

Instead, what the code is doing is performing a redirect. This creates a second request to that same page (though it's essentially irrelevant that it's the same page). This is a GET request and contains no data to send to the server.

At its simplest, you should either use AJAX or redirect the user. Currently you're mixing both.

I want to redirect to the other page.

In that case AJAX is the wrong tool for the job. You may not even need JavaScript at all, unless you want to modify the elements/values of a form before submitting that form. But if all you want is to POST data to another page while directing the user to that page, a plain old HTML form does exactly that. For example:

<form method="POST" action="example.com/artiste/mise-en-relation/">
<input type="text" name="savedArtists">
<button type="submit">Submit</button>
</form>

In this case whatever value the user enters into the <input> will be included in the POST request to example.com/artiste/mise-en-relation/ when the user submits the form.



Related Topics



Leave a reply



Submit