0

with a url of http://example.com?productName=Walkman is working right

<body>
    <h1 id="productName"></h1>
</body>


<script type='text/javascript'>
    // start by creating a function 
    function loadUp(){
        var str = window.location.search.replace(/(?:(\D+=))/ig, "") //get the search parameters from the url and remove everything before the "=" sign
        document.getElementById('productName').innerHTML = str //assign that string to the "innerHTML" of the h1 tag that has an id of "productName"
    };

    window.onload = loadUp; // once the page has loaded, fire off that function
</script>

the problem is that if i add 2 or more words in the URL
http://example.com?productName=Best_Walkman on the page i have Best_Walkman but i want to show Best Walkman

how can i hide this symbol on th website _ ? inide the tag

<h1 id="productName"></h1>
Ivan Dern
  • 47
  • 9
  • 1
    Does this answer your question? [Encode URL in JavaScript?](https://stackoverflow.com/questions/332872/encode-url-in-javascript) – Sysix May 28 '21 at 17:58
  • I'm not sure how to implement that maybe some code examples? Thank you. – Ivan Dern May 28 '21 at 18:02

2 Answers2

2

just add another replace

    str = window.location.search.replace(/(?:(\D+=))/ig, "").replace("_"," ")

edit

for all _ to be replaced, you need to replace using regex

 replace(/(?:(\D+=))/ig, "").replace(/_/g," ")
Rhea
  • 644
  • 1
  • 5
  • 14
  • very good the problem is that replaced just the first _ what if i have more than 2 words ? – Ivan Dern May 28 '21 at 18:07
  • this code right now is removing just one _ symbol, i have this Best_Walkman_Best_Walkman what is the solution for this ? – Ivan Dern May 28 '21 at 18:16
  • Problem resolved thank you str = window.location.search.replace(/(?:(\D+=))/ig, "").replace("_"," ").replace("_"," ").replace("_"," ").replace("_"," ").replace("_"," ") – Ivan Dern May 28 '21 at 18:27
  • @IvanDern i have edited my solution to replace all the _ – Rhea May 28 '21 at 18:30
  • i have one more question do you know how to make it show more than one time on the page

    the title right now is not showing in multiple places
    – Ivan Dern May 28 '21 at 18:55
  • 1
    ID is only for a single element, if you want to do it for multiple, you need to use class @IvanDern – Rhea May 28 '21 at 19:04
0
str = window.location.search.replace(/(?:(\D+=))/ig, "").replace("_"," ").replace("_"," ").replace("_"," ").replace("_"," ").replace("_"," ")

This is my solution so far, if you have something better, please post it.

Ivan Dern
  • 47
  • 9