简体   繁体   中英

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

如何通过javascript或jQuery将iframe中的所有链接设置为“ google.com”?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM