1

I am loading a page when a user clicks on a link. I want this to load within a container. This all works fine.

What I want to do is override some of the javascript variables (to stop things from happening within the loaded object).

I've tried simply re-instantiating the variables using the same name but to no avail. Any help appreciated! Is this even possible to do? When I re-instantiate the variable, nothing actually happens to the one inside the object.

 function loadURL(x){

    var url = $(x).attr('value'); // eg. http://example.com

    $('.page-loading-space').html('<object data="'+url+'"/>', function(){
        same_variable_name = null; // that gets loaded with the object
    });
    
  }

Info

A script I am running on a different domain is being called when I load the page on eg. localhost.

Here is an example:

I am developing on localhost, and I load http://stackoverflow.com into the object tag as stated above - via $('.page-loading-space').html('<object data="'+url+'"/>'); - without the callback for simplicity.

I want to clear a variable that is instantiated within this loaded object. So, if stackoverflow.com loads for example : GoogleAnalyticsObject - which it does. I want to simply nullify that object to stop it from doing any unload callbacks.

This is all for scripts implemented on my behalf - I am aware of the bad cross domain loading etc.

All I want to do is clear the value after the page is loaded into my object to stop a callback happening when it unloads. Thanks, and sorry if I wasn't clear/or still am not clear.

3
  • How are variables being passed to object? If by url, then you'd have to parse the url (medialize.github.io/URI.js) and replace the given variables (?id=5 then becomes ?id=pineapple). If not, and the object you're loading is Flash (and you're not using uri or flashvars), then you'd have to create handle within the Flash and then set them from within JS as per (stackoverflow.com/questions/7368687/…). Otherwise, could you please provide more information Commented Jul 15, 2014 at 12:06
  • Thanks for the input @eithedog. Added example of the problem. The variables aren't passed as parameters at all you see, they are loaded on the page by themselves. Commented Jul 15, 2014 at 12:17
  • I've rolled back the last edit, as there were two items that don't belong in there: solution updates (use an answer) and voting commentary (not at all please). Commented Nov 28, 2015 at 11:31

2 Answers 2

0

This should solve your problem:

window.f = '5';
$(document).ready(function(){
    var object = $('<object data="http://jsfiddle.net/"/>');
    $(object).appendTo(document.body).ready(function(){
        var doc = object.get(0).contentDocument;
        var win = doc.parentWindow || doc.defaultView;
        console.log(win, win.f);
    });
});

http://jsfiddle.net/eithe/tYu4F/

Basically, what's happening, is that when you're creating <object> element, you're creating another window and document objects for that <object>. As such, all variables from within that <object> will be bound to that window (think about <object> like <iframe>). To delete variables you can use delete operator.

Sign up to request clarification or add additional context in comments.

5 Comments

hmmm.. interesting, but I still can't nullify the javascript variables that are being pulled in with the request. I want to clear the variables being pulled in, but can't seem to access them - even to alert their values via console.
It seems you can't access cross domain javascript variables. There must be a way to achieve this....
For documents A and B (B being document loaded by object), can you please set a variable in B, before ready event, and for my code snippet change appendTo(document.body).ready to appendTo(document.body); var doc = object.get(0).contentDocument; var win = doc.parentWindow || doc.defaultView; $(win).load(. Then try accessing the variable?
Actually, scratch the above - the window won't be loaded in the moment you're trying to access it. Still, you should be able to access the properties of the window on the ready.
Ah - you're doing it via cross-domains? Yes - the same rule as for iframes applies to objects regarding cross-domain URLs.
0

It is not possible to access cross domain JavaScript variables, even in an iframe/object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.