We help IT Professionals succeed at work.

using javascript variable in html tag

elliottbenzle
on
I want to use a java script variable in my html tag, but don't know haw to do it. Can someone please show me how to make the following code work?
I'm trying to insert the value of Javascript variable "screenw" into the rel="" value of the anchor tag.

Thanks.

<script type="text/javascript">
var screenw;
var screenh;
screenw = screen.width;
screenh = screen.height;
screenw = (screenw/1.2);
screenh = (screenh/1.2);
screenw = (Math.round(screenw));
screenh = (Math.round(screenh));
</script>

<a href="26leads.swf" class="bumpbox" title="PLATINUM network for Administrators" rel="<SCRIPT type="text/javascript">document.write(screenw);</SCRIPT>-750">Leads Dashboard for Administrators <br /> </a>
Comment
Watch Question

Commented:
You need to fetch the DOM object ans insert it there.

e.g.

<script  type="text/javascript">
var screenw;
var screenh;
screenw =  screen.width;
screenh = screen.height;
screenw = (screenw/1.2);
screenh  = (screenh/1.2);
screenw = (Math.round(screenw));
screenh =  (Math.round(screenh));

document.getElementById("MyHref").rel = screenw;

</script>


<a id="MyHref"  href="26leads.swf" class="bumpbox" title="PLATINUM network for  Administrators" rel="">Leads  Dashboard for Administrators <br /> </a>


Also make sure the javascript is called after the actual href tag is inserted into the page.
(so either put the javascript into a function or place it after the href altogether)

Hope this helps...
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You had your quotes mixed up.
<script type="text/javascript">
var screenw;
var screenh;
screenw = screen.width;
screenh = screen.height;
screenw = (screenw/1.2);
screenh = (screenh/1.2);
screenw = (Math.round(screenw));
screenh = (Math.round(screenh));
</script>

<a href="26leads.swf" class="bumpbox" title="PLATINUM network for Administrators" rel="<script type='text/javascript'>document.write(screenw);</script>-750">Leads Dashboard for Administrators <br /> </a>

Open in new window

Commented:
Although that will work as well, in my opinion that is bad coding.
leakim971Multitechnician
Distinguished Expert 2019

Commented:
check this :

(work on IE, Opera, FF, Chrome, Opera and Safari)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
	var screenw;
	var screenh;
	screenw = screen.width;
	screenh = screen.height;
	screenw = (screenw/1.2);
	screenh = (screenh/1.2);
	screenw = (Math.round(screenw));
	screenh = (Math.round(screenh));
	var myAnchor = '<a href="26leads.swf" class="bumpbox" title="PLATINUM network for Administrators" rel="' + screenw - 750 + '">Leads Dashboard for Administrators<br /></a>';
//	alert(myAnchor);
	document.write(myAnchor);
</script>
</body>
</html>

Open in new window

leakim971Multitechnician
Distinguished Expert 2019

Commented:
oops wrong version, the good :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
	var screenw;
	var screenh;
	screenw = screen.width;
	screenh = screen.height;
	screenw = (screenw/1.2);
	screenh = (screenh/1.2);
	screenw = (Math.round(screenw));
	screenh = (Math.round(screenh));
	var myAnchor = '<a href="26leads.swf" class="bumpbox" title="PLATINUM network for Administrators" rel="' + parseInt(screenw - 750) + '">Leads Dashboard for Administrators<br /></a>';
//	alert(myAnchor);
	document.write(myAnchor);
</script>
</body>
</html>

Open in new window

Commented:
another option you can use

function centerWin() {
var winWidth = $(document).width();
var winHeight = $(document).height();
var popupWidth = $(".window").width();
var popupHeight = $(".window").height();

$(".window").css({
"position": "absolute",
"top": "50%",
"left":"50%",
"margin-left": popupWidth / -2,
"margin-top": popupHeight / -2
});
}

Author

Commented:
Thanks. This does help.