- Ned
- May 23, 2002
-
by Hand Knit
|
It would be best to create a string and then stick it in the page.
If you put this in the body you have a div to write your string to.
code:<div id="output"></div>
Then you need to have javascript that writes to that div. This code looks for an element with the id of output and sets the string variable as the inner HTML of the element.
code:document.getElementByID('output').innerHTML(string);
But if you do this with your current code you run into a small problem. The div doesn't exist when the code is run.
The simplest solution for this is to call the function after you know the div exists. If you wrap your code with a function and call it rollDice, you can stick a piece of javascript after the element.
code:<script type="text/javascript">
rollDice();
</script>
I prefer to do things unobtrusively. Usually this involves onload type statements.
Of course, all of this is much easier if you pick up jQuery.
|
#
¿
Feb 8, 2009 07:41
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 30, 2024 01:12
|
|
- Ned
- May 23, 2002
-
by Hand Knit
|
Can you turn it into a function that you call on load and on resize?
|
#
¿
Aug 23, 2009 06:02
|
|
- Ned
- May 23, 2002
-
by Hand Knit
|
Switch to jQuery. It is pretty much the standard now.
|
#
¿
Dec 31, 2009 06:03
|
|
- Ned
- May 23, 2002
-
by Hand Knit
|
How did you link to jQuery?
use wp_enqueue_script('jquery'); and you should be good. It might even be included if you use a bunch of plugins. Just remember that it gets loaded with the no conflict stuff there.
|
#
¿
Apr 3, 2010 22:56
|
|
- Ned
- May 23, 2002
-
by Hand Knit
|
Grumpicat posted:
I put
code:<?php wp_enqueue_script('jquery'); ?>
into the header but I don't think it's doing anything..
Write your code like this when you include it that way.
code:<script type="text/javascript">
jQuery(function($) {
alert('hi');
});
</script>
|
#
¿
Apr 4, 2010 15:38
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 30, 2024 01:12
|
|
- Ned
- May 23, 2002
-
by Hand Knit
|
code:<html>
</head>
<body>
<img src="blank.jpg" id="image">
<head>
<script type="text/javascript">
if(location.href == "www.mysite.com/index.html")
{
document.getElementById('image').src = "someimage1.jpg";
}
else if (location.href == "www.mysite.com")
{
document.getElementById('image').src = "tsomeimage2.jpg";
}
</script>
</body>
</html>
You need to run it after the image id actually exists in the page.
But you shouldn't be doing this with JS
|
#
¿
Dec 21, 2011 06:08
|
|