简体   繁体   中英

Changing all links on page with js

I want to remove "/index.php" in all links on page

Example:

http://example.com/?hostname=sad2.cherobr.ru&path=/index.php/o-nas

change to:

http://example.com/?hostname=sad2.cherobr.ru&path=/o-nas

with plain js

var allAnchors = document.querySelectorAll("a");
Array.prototype.slice.call( allAnchors ).forEach( function( el ){
  var href = el.getAttribute( "href" );
  el.setAttribute( "href", href.replace( "/index.php", "" ) );
});

使用替换功能。

 console.log('http://example.com/?hostname=sad2.cherobr.ru&path=/index.php/o-nas'.replace('/index.php', ''));

You can do it using the replace("url","/index.php") function like this.

var ref= document.getElementById("linkId").href;
document.getElementById("linkId").href= ref.replace("/index.php","");

For removing it from all links on the page.

var refs= document.getElementsByTagName(a);
for(i=0;i<refs.length;i++)
{
  refs[i].href= refs[i].href.replace("/index.php","");

}

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