Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:22:06+00:00 2026-06-10T23:22:06+00:00

I’m trying to extract hash substring values from window.location and write them into HTML

  • 0

I’m trying to extract hash substring values from window.location and write them into HTML for the user to see, with an URL syntax that disallows use of ? as query string delimiter.

So I am working with this simple page:

<html>

<input type="button" id="test" value="Test" />
<!-- input buton is here just to test the script -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>

<script>
$(function() {
    $('#test').click(function() {
        window.location = (window.location);
        GetURLParameter('source');  
    });
});

function GetURLParameter(sParam) {
    var sPageURL = window.location.hash.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            alert(sParameterName[1]);
            return sParameterName[1];
        }
    }
} 
</script>

</html>

I call the page with an URL that looks like this, this the client window.location:

http://example.com/pagename.html#source=FOO&medium=BAR&campaign=FRED

I want to write FOO, BAR, FRED into the page, so the user can see it.

(notice no ? in the URL as query string delimiter, just #)

… On input button click, I get a successful alert (as expected) that shows the source “FOO” from the URL. Great.

But how do I get the other two SParamaterNames, and then how do I write them into the page as HTML for the user to see, rather than return an alert?


UPDATE UPDATE UPDATE (below)

Thanks, @sam-baker!! … Great answer. I used your first example, but changed the first two lines in your code to $(window).load(function() { so that the script runs onload, which was my intent.

So, I am still using this same URL:

http://example.com/pagename.html#source=FOO&medium=BAR&campaign=FRED

But I want to insert the hash substring values into hardcoded HTML, like this:

<body>
<span class="style1">$need_source_value_here</span><br />
<span class="style1">$need_medium_value_here</span><br />
<span class="style3">$need_campaign_value_here</span><br />

I will style each value individually, by hardcoding span or div styles into the HTML.


UPDATE UPDATE UPDATE

Here’s the solution:

Hit the following simple page in the code example bellow with this URL syntax, and it will output the substring vars to the user, onload, wherever you want them on the page.

http://example.com/pagename.html#dyntext=FOO+MAN+CHU&dynterm=BAR+HOPPING&dynimage=FRED+IS+DEAD



<html>
<body>

<span id="dyntext-span" style="font-weight: bold;"></span><br />
<span id="dynterm-span" style="font-style: italic;"></span><br />
<span id="dynimage-span" style="text-decoration: underline;"></span><br />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>

<script>
$(document).ready(function() {
        var tags = ["dyntext", "dynterm", "dynimage"];
        for (var i = 0; i < tags.length; ++i) {
            var param = GetURLParameter(tags[i]);
            if (param) {
                var dyntext = GetURLParameter('dyntext');
                var dynterm = GetURLParameter('dynterm');
                var dynimage = GetURLParameter('dynimage');
            }
        }

        var elem = document.getElementById("dyntext-span");
        var text = document.createTextNode(dyntext);
        elem.appendChild(text);
        var elem = document.getElementById("dynterm-span");
        var text = document.createTextNode(dynterm);
        elem.appendChild(text);
        var elem = document.getElementById("dynimage-span");
        var text = document.createTextNode(dynimage);
        elem.appendChild(text);     
});

function GetURLParameter(sParam) {
    var sPageURL = window.location.hash.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}

</script>

</body>
</html>

Had some crossbroswer issues because innerText is not supported by Firefox, did not want to deal with cross-browser fallback to textContent, etc, so just walked DOM tree and created text nodes, replacing the + unicode character with space. Hope this is useful to you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:22:08+00:00Added an answer on June 10, 2026 at 11:22 pm

    You’re only requesting the first of the 3 parameters using:

    GetURLParameter('source');
    

    If you want all 3 parameters and you know the names, you can use:

    var source = GetURLParameter('source');
    var medium = GetURLParameter('medium');
    var campaign = GetURLParameter('campaign');
    

    To add those vars to the page, you can insert them in divs like this:

    <input type="button" id="test" value="Test" />
    <!-- input buton is here just to test the script -->
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
    
    <script>
    $(function() {
        $('#test').click(function() {
            // Iterate through a known list of tags
            var tags = ["source", "medium", "campaign"];
            for (var i = 0; i < tags.length; ++i) {
                var param = GetURLParameter(tags[i]);
                if (param) {
                    var div = document.createElement('div');
                    div.innerText = param;
                    div.className = "query-string-param";
                    document.body.appendChild(div);
                }
            }
        });
    });
    
    function GetURLParameter(sParam) {
        var sPageURL = window.location.hash.substring(1);
    
        console.log(sPageURL);
    
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) {
                return sParameterName[1];
            }
        }
    } 
    </script>
    
    </html>
    

    That version queries each of the 3 tags and creates a new div in the page for that tag with the tag’s value as the content.

    It also adds a CSS class, query-string-param to those divs so that you can style the text – presumably you’ll want to do that, and add more HTML to each div since it’s not clear why that text is displayed.

    If you don’t know the query string keys upfront (source, medium and campaign), you could use a function I created, GetAllURLParameters that returns an array of objects with both the key (“source”) and the value (“FOO”) for each param.

    That would look like this:

    <input type="button" id="test" value="Test" />
    <!-- input buton is here just to test the script -->
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
    
    <script>
    $(function() {
        $('#test').click(function() {
            // Iterate through a known list of tags
            var params = GetAllURLParameters();
            for (var i = 0; i < params.length; ++i) {
                var param = params[i];
                var div = document.createElement('div');
                div.innerText = "Value of param '" + param.key + "' is '" + param.value + "'";
                div.className = "query-string-param";
                document.body.appendChild(div);
            }
        });
    });
    
    function GetURLParameter(sParam) {
        var sPageURL = window.location.hash.substring(1);
    
        console.log(sPageURL);
    
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) {
                return sParameterName[1];
            }
        }
    } 
    
    function GetAllURLParameters() {
        var sPageURL = window.location.hash.substring(1);
        var params = [];
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split('=');
            params.push({
                key: sParameterName[0],
                value: sParameterName[1]
            });
        }
        return params;
    }
    </script>
    
    </html>
    

    Which then shows the names and values of all query parameters.

    UPDATE

    Since you already have hardcoded HTML that looks like this:

    <span class="style1">$need_source_value_here</span><br />
    <span class="style1">$need_medium_value_here</span><br />
    <span class="style3">$need_campaign_value_here</span><br />
    

    The easiest way to insert your values would be to add an id to each of those spans:

    <span id="source-span" class="style1"></span><br />
    <span id="medium-span" class="style1"></span><br />
    <span id="campaign-span" class="style3"></span><br />
    

    Then after you query the parameters, you can insert them like this:

    var source = GetURLParameter('source');
    document.getElementById('source-span').innerText = source;
    var medium = GetURLParameter('medium');
    document.getElementById('medium-span').innerText = medium;
    var campaign = GetURLParameter('campaign');
    document.getElementById('campaign-span').innerText = campaign;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.