简体   繁体   中英

Replacing text on web page using HTML or Javascript

I am customizing an HTML page provided and hosted by a third-party, so I am severely restricted in what I can do to the page. On the page there is a navigation menu that looks like this.

<ul class='nav'>
   <li class='active'>
     <a href='/affiliate'>
       <span id='affiliate-nav-homepage'>
         <span class='default'>Home Page</span>
       </span>
     </a>
   </li>
   <li>...

Question: How can I make the above snippet display the text "Home" instead of "Home Page" , if all I am allowed to do is add a <style> element in the page <head> ; and add some HTML code (which may include Javascript) near the start of the page?

I have a considered going down one of these two paths, but both paths are problematic.

1) Use CSS to hide their text ("Home Page"). Add my own text ("Home") using :before or :after pseudo-selectors.

display: none is probably not a good way to hide the text, because browsers that don't understand :before and :after will still understand display: none and I will end up with no text at all.

Are there better CSS alternatives for me? Change font size to 0? change text color? Manipulate z-index somehow? It is OK if some older browsers display the text "Home Page". It is not OK if some browsers display "Home Page Home", or if some browser do not display any text at all.

-- OR --

2) Use Javascript to manipulate the DOM

The difficulty with Javascript is that I can only insert it near the start of the page body before the elements that I want to modify. I could hook an event that fires after the entire page is loaded, but I want to avoid the page text flashing (the text "Home Page" being displayed briefly and then changing to "Home"). Is there such an event and how would I hook it?

Thank you for you help.

In JavaScript

window.addEventListener("load", function(){
    document.querySelector("span.default").textContent = "Home";
    //using document.querySelector to select the span with classname default
    //using textContent to change the content of the node.
}, false);

In a script block will do it.

This will execute on page load. This will be executed so fast that in almost all cases you won't see text jumping.

Example:

 window.addEventListener("load", function(){ document.querySelector("span.default").textContent = "Home"; }, false); 
 <ul class='nav'> <li class='active'> <a href='/affiliate'> <span id='affiliate-nav-homepage'> <span class='default'>Home Page</span> </span> </a> </li> </ul> 

When your page loading becomes slowed by other resources, @Mateusz Mania's idea of using CSS to hide the element and bring it up again is actually pretty nice. I would suggest using opacity: 0 or visibility: hidden instead of display:none since the latter will remove the element from the page and when it becomes visible again it will make the content jump down below it.

Exploring the CSS option

 #affiliate-nav-homepage:before{ content: "Home"; } .default { display: none; } 
 <ul class='nav'> <li class='active'> <a href='/affiliate'> <span id='affiliate-nav-homepage'> <span class='default'>Home Page</span> </span> </a> </li> </ul> 

Anti flashing fix:

.default { display: none; }

Changing text and showing, when document is ready:

body.onload = function (){

    //getElementsByClassName not working in old browsers, so ...
    var el = document.getElementById('affiliate-nav-homepage').getElementsByTagName('span')[0];

    el.innerHTML = 'Home';
    el.style.display = 'block';
};

Additional You can hide all content and show it after all javascript changes by this method.

This is not elegant solution, but as You say, You have limited options to resolve Your problem.

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