1

I have the following code:

<!DOCTYPE html>
<html>
 <body>
  <table border="1">
   <tr>
    <td >row 2, cell 1</td>
    <td style="white-space:nowrap;"><a href="http://aa.bb.cc/<script type="text/javascript"><!--
     var date = new Date();
     var d  = date.getDate();
     var day = (d < 10) ? '0' + d : d;
     var m = date.getMonth() + 1;
     var month = (m < 10) ? '0' + m : m;
     var yy = date.getYear();
     var year = (yy < 1000) ? yy + 1900 : yy;
     document.write( year + month + day);
     //--></script>>  Click to check</a></td>
   </tr>
  </table> 
 </body>
</html>

What I want is that when I press :click, the browser should go to the following link: http://aa.bb.cc/yyyymmdd

yymmmdd is obtained through the Javascript and displays the current year, month date.

The Javasript works, but I can't add the result of it to the URL of the a href attribute.

2
  • 2
    Bad! Do not try to embed Javascript with a HTML element. Try creating the whole anchor element instead within the Javascript and then adding that to your page. Commented Jan 17, 2014 at 21:06
  • Yes, a script is an element, not an attribute. And if it would have been an attribute, you still couldn't have have put it inside another attribute. And the quotes are messed up. Commented Jan 17, 2014 at 21:48

3 Answers 3

2

Why not separate your javascript from your html, by putting it into the head element. Then once the document has loaded onload call that function to append it to your links href.

<!DOCTYPE html>
<html>
<head>
<script  type="text/javascript">
function appendDateToURL(){
  var date = new Date();
  var d  = date.getDate();
  var day = (d < 10) ? '0' + d : d;
  var m = date.getMonth() + 1;
  var month = (m < 10) ? '0' + m : m;
  var yy = date.getYear();
  var year = (yy < 1000) ? yy + 1900 : yy;
  var myLink = document.getElementById("myLink");
  myLink.setAttribute("href",(myLink.href+ year + month + day));
}
</script>
</head>
<body onload="appendDateToURL();">
<table border="1">
<tr>
<td >row 2, cell 1</td>
<td style="white-space:nowrap;"><a id="myLink" href="http://aa.bb.cc/">  Click to check</a></td>
</tr>
</table> 
</body>
Sign up to request clarification or add additional context in comments.

Comments

0

I just added a jsfiddle. Took your code and added just a couple extra line.

<a class="generated-link" href="">Link me!</a>

<script>
var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;

var link = document.querySelector('.generated-link');
link.href = year + month + day;
</script>

http://jsfiddle.net/XvM5N/

Comments

0

Here is an example of creating the a element through the Javascript and then appending it to the page. In the future do not try to put a script tag within the middle of another tag. Obviously you don't want to append it to body in your example, but I will leave the appending to you.

var anchor = document.createElement("a");
var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
anchor.innerHTML = "Click to check";
anchor.href = "http://aa.bb.cc/" + year + '' + month + '' + day;
document.body.appendChild(anchor);

JSFiddle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.