Safari & Javascript Policy - Same Origin?

I have Javascript that changes the host in the links to match the current dev / test server.

Here's an example:

var ndomain = document.domain;
var mydomain = 'www.foo.com';
var alink = document.getElementsByTagName('a');
for (var i = 0; i < alink.length; i++) {
    if (alink[i].href.length > 0){
        if (alink[i].host.substr(0, mydomain.length) == mydomain){
            alink[i].host = ndomain;
        }
    }
}

      

This changes the links from http://www.foo.com/page.html to http://level1.test.foo.com/page.html .

This works in every browser I've tested except Safari (Mac or Win). I have searched and searched for information on why and the closest reason I came up with is the "one origin" policy.

Based on my understanding of the same origin policy, this should work because everything is under the foo.com domain. Could Safari be more restrictive about switching to a two additional subdomain (like alvel1.test)?

Can anyone please advise why this process is not working in Safari or how can I get it to work in Safari?

TIA!

0


source to share


2 answers


It shouldn't have anything to do with the same origin policy.

The same can be seen in Google Chrome. Both use WebKit, so perhaps the WebKit DOM interface is the key to the problem.

Running the JS in the question on Chrome does not change the hosts in the links. Neither the JS Debugger nor the JS Console reported any issues.

Attempts to change the anchor.host property:



  • no error reported
  • without changing the anchor.host property

This suggests that the anchor.host property is read-only for some reason.

The anchor.href property appears for the record, so this is probably your best option. The following code will work:

var ndomain = document.domain;
var mydomain = 'www.foo.com';
var alink = document.getElementsByTagName('a');
for (var i = 0; i < alink.length; i++) {
    if (alink[i].href.length > 0){
        if (alink[i].host.substr(0, mydomain.length) == mydomain){
            var currentHref = alink[i].getAttribute('href');
            var newHref = '';

            // Generate newHref based on currentHref and setting host as required

            alink[i].setAttribute('href', newHref);
        }
    }
}

      

0


source


You have a syntax error on line 5 (3 opening brackets, only 2 closing brackets).



0


source







All Articles