Ivan Dern
Ivan Dern

Reputation: 47

JavaScript - h1 replace based on url parameters javascript, how to add more than one word in url and on the page?

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>

Upvotes: 0

Views: 62

Answers (2)

Ivan Dern
Ivan Dern

Reputation: 47

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.

Upvotes: 0

Rhea
Rhea

Reputation: 644

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," ")

Upvotes: 2

Related Questions