views:

45

answers:

2

how to set all links in iframe to "google.com" by javascript or jQuery ?

+1  A: 

Run this in iframe

$("a").attr("href", "http://www.google.com/" + $("a").attr("href"))

Example: jsfiddle


EDIT

If you need to replace the domain links with google.com, use this

$("a").each(function(){
    var pathname = $(this)[0].pathname;
    $(this).attr("href", "http://www.google.com" + pathname);
});

Example: jsfiddle

Topera
but how can i run the code in iframe ?
faressoft
A: 

You can use the iframe's contentWindow attribute to get a hold on the global object running in the iframe. From there you can do what you want.

$(iframe.contentWindow).find("a").each(function(){
  //replace url
})

But if url of the iframe is on another domain, this will trigger a XSS (cross server scripting) error and you won't be able to access the window dom. This is a security feature, so it cannot be circumvented. In other words, you cannot to that.

Joeri Hendrickx