Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
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.

Adbot
ADBOT LOVES YOU

Ned
May 23, 2002

by Hand Knit
Can you turn it into a function that you call on load and on resize?

Ned
May 23, 2002

by Hand Knit
Switch to jQuery. It is pretty much the standard now.

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.

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>

Adbot
ADBOT LOVES YOU

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply