DillPixel
DillPixel

Reputation: 965

Transfer variables through link and use them in the new page (JavaScript)?

I only know a few basic things in JavaScript, so I need some help with this...

What I want to do is: Have a link open a new window and in that window use 2 variables passed to edit some content. Change some text to the first variable, and change a link in that new window to the value of the second variable.

Is this possible? I'm not looking for exact code, just any resources I can look at would be just fine.

Thank you.

Upvotes: 0

Views: 218

Answers (6)

Rogel Garcia
Rogel Garcia

Reputation: 1915

To open the window:

var myObject = new Object();
myObject.param1 = 'A';
myObject.param2 = 'B';
var windowPopup;
if(window.showModelessDialog){
    windowPopup = window.showModelessDialog(path, myObject, '');
} else {
    windowPopup = window.open(path, '_blank', '');
    windowPopup.myObject = myObject;
}

To read the contents in the new window:

var myObject = window.myObject; 
if(!myObject){
    if(window.dialogArguments){
        myObject = window.dialogArguments;
    }
}
alert(myObject.param1);

This is a crossbrowser solution and works for IE9.

Is is also possible to pass a callback for the second window. Example:

var myObject = new Object();
myObject.param1 = 'A';
myObject.param2 = 'B';
myObject.callback = function(param) {document.forms[0].field.value = param;}

When you get the myObject reference in the new window do:

 myObject.callback('value from the new window'); 

The value will be changed in the original window.

Upvotes: 1

Minko Gechev
Minko Gechev

Reputation: 25672

I'm not sure that I've understood exactly what you mean but here is a script which allows you to edit the content of the new window when typing into the old one: http://jsfiddle.net/Jpt3C/3/

JS

var newWindow;
$('#foo').click(function () {
    newWindow = window.open();
});
$('#bar').keydown(function () {
    if (newWindow) {
        newWindow.document.body.innerHTML = this.value;
    }
});

HTML

<a href="#" id="foo">Click me</a>
<input type="text" id="bar" />

Upvotes: 0

O&#39;Rooney
O&#39;Rooney

Reputation: 3100

Although this might technically be possible, it's not really suitable for the web and leads to security weaknesses and behaviour that users won't expect and that browsers may block. Think of each browser window as an independent application. You would be better off using a dynamically created div (or similar) within the same page/window.

Perhaps you could explain more about WHY you think you need this.

Upvotes: 1

devdRew
devdRew

Reputation: 4571

Yes, it's possible by opening aa child window by:

var popup = open(URL, 'Title', 'width=1000,height=420,scrollbars');

And then you can access vars in parent window from opened one by:

parent.var = 'some value';

Hope that is exactly you need.

Upvotes: 0

Jakub
Jakub

Reputation: 20475

By doing this type of approach you open yourself up to cross site scripting attacks. You really need to be careful as this data might lead to injections, or defacing.

Upvotes: 1

Heldraug
Heldraug

Reputation: 256

Perhaps this will be useful: http://papermashup.com/read-url-get-variables-withjavascript/ I think that what you are trying to accomplish to be better done with a server side scripting language such as PHP.

Upvotes: 0

Related Questions