0

I'm a newbie to HTML. I googled the question and tried some code but it didn't work so I'm asking here for help.

What I am trying to achieve: replacing the ?? in the HTML paragraph with a value from a URL (for example: http://mysite.html?name=Ryan)

<p> Hello ??<br> Thanks for answering my question</p>

My Questions: 1. This Javascript should extract Ryan from the URL - is it correct?

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript">
    // Parse the URL parameter
    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
    // Give the parameter a variable name
    var urlVar = getParameterByName('name');
});
</script>
  1. If the code in Question 1 is correct, how do I place the value in urlVar (which is Ryan) instead of the ?? inside the HTML paragraph?
4
  • 3
    Would make more sense to have a span element with an id in the paragraph and replace it.... Commented Aug 30, 2017 at 17:33
  • Is urlVar the value you expect it to be or not? It's not really clear to me specifically what's being asked here. Commented Aug 30, 2017 at 17:33
  • @yuriy636 I think the javascript code extracts Ryan, my question is how to place the value of Ryan inside the HTML paragraph so it will render on the browser: "Hello Ryan, Thanks for answering my quesiotn" Commented Aug 30, 2017 at 17:34
  • This is pretty much what you are looking for: stackoverflow.com/questions/15618470/… Only thing is that you would need to find a way to select the paragraph despite it not having an id or class. (Or adapt your HTML like the suggested comment or the answer below). Commented Aug 30, 2017 at 17:42

2 Answers 2

0

Do you have a hashbang in the URL? You can try this out:

    function getParameterByName = function(name, useHash) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(useHash ? location.hash : location.search);
        return results === null ? undefined : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

Passing true or false as the useHash parameter

Sign up to request clarification or add additional context in comments.

Comments

0

I assume the HTML is already rendered, after which the javascript is being used.

In which case you need to have a handle to replace it.

<p> Hello <span id="username">??</span> Thanks for answering my question</p>

Now within the Javascript code you can use something like:

var urlVar = getParameterByName('name');
 document.getElementById('username').innerHTML = urlVar;

EDIT: <p> Hello <span id="username">??</span> Thanks for answering my question</p>

uses Jquery document.ready to execute the code after the DOM as been rendered

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
      // Parse the URL parameter
    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
    // Give the parameter a variable name
    var urlVar = getParameterByName('name');
    document.getElementById('username').innerHTML = urlVar;
});
</script>

8 Comments

copy paste your code to my site and it didn't work - any ideas why?
Probably because his remake of your function takes one parameter and the original two?
Not sure i might have made a mistake. Though within the code you posted i did see }); at the end of your code. its something that shouldn't if the code you posted is everything there is. Also your code is never executed if thats every thing you have.
@NineMagics No his code can actually use only the 'name' parameter from what i can see. if no url is specified it takes the current url.
@spoofie The script does extract the url variable value (i.e Ryan) but it doesn't place it instead of ?? in 'username'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.