简体   繁体   中英

jQuery - Change all URLs on page include CSS & JavaScript files

Is there any way to change all URLs on page, include URLs in CSS/JavaScript files?

This is what I want to do:

HTML file:

    Before:
    <a href='../foo.html'></a>
    After:
    <a href='http://my.proxy.server/?url=http://absolute.path.to.page/foo.html'></a>

CSS file:

    Before:
    .bar {background-image: url(../foo.png);}
    After:
    .bar {background-image: 
        url(http://my.proxy.server/?url=http://absolute.path.to.image/foo.png);}

JavaScript file:

    Before:
    window.open('../foo.html')
    After:
    window.open('http://my.proxy.server/?url=http://absolute.path.to.page/foo.html')

I know it's easy to change URLs of HTML tags with jQuery, but how about URLs in CSS & JavaScript files?

$("*").each(function(){
   if($(this).attr("href"){
     $(this).attr("href","'http://my.proxy.server/?url=http://absolute.path.to.page/foo.html");
   }
});

And repeat that for "src"

try to convert all html to string (something like content = $("html").html() )
then make 3 or more regular expressions OR if you need to replace only ../ you can use .split.join:
content.split("../").join(" http://my.proxy.server/?url=http://absolute.path.to.page/ ");
and then back content $("html").html(content);

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