Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this javascript code:

<script ="text/javascript">
    var newUrl=window.location.href;
    var pathArray="";
    pathArray=newUrl.substr(newUrl.lastIndexOf("?")+1);
</script>

I want to use the pathArray variable as a part of my href on the tag

This is my html code

<a href="game.html?"+pathArray>
  <img src="img/RestartButton.png" style="position:absolute;
  left:80px; top:220px">
</a>

but it seems like it doesn't read the value if the variable, but the name of it instead.

share|improve this question
 
Sorry, but you can't just dump the name of a Javascript variable anywhere in your HTML markup and expect it to work. –  Lightness Races in Orbit Jul 27 at 5:39
 
Wouldn't the pathArray variable will be a global variable, because it's on the HTML file? –  Adyana Permatasari Jul 27 at 5:48
1  
HTML and Javascript are two different things... HTML doesn't have variables. It certainly doesn't have Javascript's. HTML also doesn't have an + operator for arguments. It's a markup language, not a programming language. –  Lightness Races in Orbit Jul 27 at 5:59
add comment

5 Answers

You're mixing your javascript into your HTML.

I believe your pathArray variable will also not contain what you are expecting.

Try this in your script tag:

var gamePath = "game.html?" + window.location.search.replace( "?", "" );
document.getElementById("gameAnchor").setAttribute("href", gamePath);

And add an id to your anchor:

<a href="#" id='gameAnchor'>

The javascript will get all GET parameters from the current url and then replace the href attribute in the element with an id of gameAnchor with your game.html concatenated with the GET parameters in the url.

share|improve this answer
add comment

You will have to use JavaScript for this as well. First give your anchor an ID:

<a id="myLink" href="game.html?"><img src="img/RestartButton.png" style="position:absolute; left:80px; top:220px"></a>

And then add following to your JavaScript code:

document.getElementById('myLink').href += pathArray;

The code will add the content of your string variable to href property of anchor element.

share|improve this answer
add comment

First, make the anchor easier to select by giving it an identifier.

<a href="#" id="pathLink"></a> (This is assuming there is more than one anchor on your page)

Then, in your script above include:

var anchor = document.getElementById("pathLink");

The anchor tag has a native href property, so assigning the new one is as easy as this:

Rewrite:

var anchor = document.getElementById("pathLink").href += pathArray;
share|improve this answer
add comment

It won't work like that. You will have to loop through the anchors your need using JavaScript.

This hasn't been tested. I'm assuming you want this to happen to more than one anchor link.

HTML:

<a class="updatethis" href="game.html">...</a>
<a href="not-game.html">...</a>
<a class="udpatethis" href="game.html">...</a>

JavaScript: var anchors = document.getElementsByTagName('a');

for (var i = 0, a; a = anchors[i++]; ) {
    if (a.className === 'updatethis') {
        a.href += window.location.search;
    }
}
share|improve this answer
add comment

You can't place a javascript variable anywhere in html. Instead you need to get the dom element through script and append the href attribute. give your anchor an id or classname and try this as an example.

<a id="myLink" href="game.html">
    <img src="img/RestartButton.png" style="position:absolute;left:80px; top:220px">
</a>

<script type="text/javascript">;
    document.getElementById('myLink').href += window.location.search
</script>
share|improve this answer
1  
Instead of dumping code on the OP, why not explain the solution? –  Lightness Races in Orbit Jul 27 at 5:43
 
I've tried that, but it keeps giving me this error: Uncaught TypeError: Cannot read property 'href' of null at file:///android_asset/www/lose.html?level1:8 –  Adyana Permatasari Jul 27 at 5:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.