1

I need to add some JavaScript into my HTML.

I have a JavaScript variable. How can I add it to my HTML code?

<a href="index.php?something=MYJAVASCRIPT VARIABLE">

UPDATE: Tried this:

<a href="#"  onclick="window.location.href = 'index.php?month=' +thisyear+'-1-1'" rel="external"> Jan </a>

But it's not working for some reason...

4 Answers 4

2

Adding the js contents of the variable into a $_GET parameter for PHP to use.

<script type="text/javascript">
    var myvar = 'THIS_IS_MY_JS_CONTENT';
</script>

<a href="#" onclick="window.location.href = 'something.php?variable=' + myvar">Variable</a>

View it live here: http://jsfiddle.net/kuroir/kmquk/

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

2 Comments

Trying this but there's an error somewhere ? As it's not working: <a href="#" onclick="javascript:window.location.href = 'index.php?month=' +thisyear+'2011-1-1'" rel="external"> Jan </a>
<a href="#" onclick="javascript:window.document.location = 'index.php?month=' + thisyear + '2011-1-1'" rel="external"> Jan </a>
1

You can propably use something like :

  <a href="#"
     onclick="window.location.href='index.php?something='+yourJavascriptVariable"/>

1 Comment

Please note that it is not onclick="javascript:window.location.href= but rather onclick="window.location.href=.
-1

you may write it like this:

<div id="content"></div>

and your js is <head>

$(function(){
  var name = "Pr0fess0rX";
  $("#content").html("<div><h3>" + name + "</h3></div>");
});

Comments

-1

You can give ID to h3 tag or find it by jQuery and give value of variable to innerText property of found control.

For example <div><h3 id="mytagID"></h3></div>

function DoSomething() {
   var myTag = document.getElementById("mytagID");
   myTag.innerText = myvariableValue;
}

Comments

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.