Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refreshing a Div tag 1

Status
Not open for further replies.

DonHawaii

Programmer
Jul 27, 2006
35
US
I actually want to do 2 things
1 - Change the src for a script then
2 - Refresh just the div it resides in

The src doesn't seem to be a problem but how can I just refresh the div and not the page?

I tried innerHTML but can't seem to get JS to write JS into the div
 
Inside the Div is a javascript that has its source elsewhere. There are actually 3 js files I want to rotate in there.

So 1st, change the src. Then refresh the div tag so the js reloads without reloading the entire page.

<div id="holder">
<script language = "javascript" src="whatever.js"></script>
</div>

Change the src and reload "holder" so it runs the script again.
 
to do what you want you'll need to modify your javascript, creating a function. here's a very simple example:

Code:
function writeStuff(strToWrite) {
    var myDiv = document.getElementById("div1");
    myDiv.innerHTML = "<h1>" + strToWrite + "</h1>";
}

then, you'd have your html, I assume something like this:

Code:
<div id="div1"></div>

you would initially call the function from some sort of an event - like when the page loads:

Code:
<body onload="writeStuff('Hello World!');">

and you could then change it in that manner:

Code:
<input type="button" onclick="writeStuff('here I am!');" name="btnClickMe" value="Click Me" />

This is the traditional way of doing what you're attempting. Does it make sense to you?



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
So how do I have it write the javascript?

It doesn't want to accept this:


myDiv.innerHTML = "<script src='something.js'></script>"

I put that in and it throws an error.
If that would work then I could just change the src each time and throw a timer on it.
 
But it isn't all MY js. [wink]

Like one is a random quotes, another is random trivia, and so on. But they all have their js on their own host sites.

Thus instead of having all of these on the page at the same time, I want to switch which one is there every so often.

 
True, but to get it to change I would have to refresh the entire page. I just want to refresh the Javascript.

Here is a classic example. Banner ads. Let's say I am receving banner ads from an outside source, maybe Google. Say I want to change ads WHILE the person is still on the page without refreshing the page.

How can I call the javascript (which belongs to Google) again? It is the same thing as what I'm asking. I thought that refreshing a DIV tag would work but it looks like I have to write the js to the page again.

So how do I write a script to the page using javascript?
 
That really depends on the script. For example, if the script uses document.write to output content while the page is loading, and you call it after the page has loaded, you're going to break your page (all the content will be overwritten).

Perhaps you could give us some solid code rather than hypothetical what-ifs, as we can give a specific answer rather than general advice about circumstances that may never happen.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
if you wanted to change banners, you would not change the javascript source. you would properly code a function to recall itself every x amount of seconds, which would load a new banner ad in the specified location, rather than "reloading the javascript".



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Do the scripts have to be on the same page or can you use an iframe?
Code:
<iframe name="banner"></iframe>
<script>
function loadBanner(src){
  window.frames["banner"].document.open();
  window.frames["banner"].document.write("<scr"+"ipt src='" + src + "'></scr"+"ipt>");
  window.frames["banner"].document.close();
}
</script>
If the content needs to be in the same page, maybe you could use a hidden iframe as a buffer and copy the content into a div when the iframe is loaded.

Adam
 
Why do I get the feeling I'm talking to a wall?

What part of I DON'T OWN THE JS FILE is not clear?

I have this:

<script src='
It delivers something to my website. Possibly a banner ad, or a random quote or joke or something.

What I want to do is REFRESH it so that I get a NEW one. I have a timer script but I can not get it to refresh the js I'm calling.

SOOooooo

What I tried to do was this:

In the page:

<div id="myDIV">
<script src='</div>

Then I have my timer script do this

<script>
function refreshDIV() {
myDIV.innerHTML = "<script src='}
</script>

But what I get is an error and the ";} displayed. So the script does not want to write javascript to the div tag.

Summary:
I want to refresh the DIV tag so that the js reruns and gives me a new result. I do not have control of the js so I can't write a timer into the js file itself thus I must write a timer function that somehow refreshes the contents of a div tag.
 
as BRPS has said, and as we've been suggesting all along, the code is most likely written out with a document.write() line. because of this, you can not simply re-call the document.write() function, as that will flush your page, causing nothing to appear (except what it's writing). you will most likely need to reload the entire page.

you feel like you're talking to a brick wall because this is how the conversation is going:

Q: how can i do this?
A: that's not the correct way to do something. do this instead: <example>
Q: how do i do it my way?
A: that's not the right way to do it. do it this way instead: <another example>
Q: why aren't you listening?

we're here to tell you the best way to do things, and help you fix your errors. you haven't provided us with any javascript, and you aren't taking our advice.

that's why it feels like you're talking to a brick wall.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Not to mention cLFlaVA is, in fact, a brick wall.

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
No the conversation is going like this:

Q: How do i do this?
A: Totally off the topic
Q: Back to what I was asking
A: Again off the topic.

You keep telling me the best way to do it is write the timer into the js but I don't have control over the js. Then you say the same thing again.

Finally you seem to be realizing that I don't have control over the js and giving the answer "You can't do that because ..."

At last. That makes some since. Glad you finally realized I don't have control over the JS.

So now, the correct answer is coming out. Possibly this can be done with an I-Frame instead.
 
who owns this js? is it free? are you hotlinking it? anyway, you can easily view the js file and make it your own (html, js and css are cool like that). then you'd have complete and total control over the js file.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
But I'm wanting to put in a BUNCH of them from different sources.

Like I originally said.

Say when a person first comes in they see a random quote. Then 30 seconds later it changes to a "on this day in history" item, then 30 seconds later it has a banner ad, and so on and so forth.

So I have to change the src and refresh the div tag to get the new js to run.

Since this doesn't sound feasible, I will look at doing it in an I-Frame. Then refresh the document in the I-Frame instead.
 
First off let me say that you're probably going about this 110% the wrong way - hence the reason everybody else is trying to offer different suggestions. Having javascript populate a div tag via document.write (which is what you're probably doing) is bad news - it will not work dynamically, so your solution will likely never work.

Now, as to why you're getting an error. The likely reason is that you are not breaking up your <script> tags when you reset the innerHTML. You probably need to fragment the string and concatenate it when it's being set. Like this:
Code:
<script>
function refreshDIV() {
myDIV.innerHTML = "<scr[!]" + "[/!]ipt src='[URL unfurl="true"]http://www.awebsite.com/something.js'></scr[/URL][!]" + "[/!]ipt>";
}
</script>

Like I said above, it's still probably not gonna work like that - but it's likely that you'll stop getting the error. You'd be best off copying the js file into one of your own directories so that you can modify it. Or even better, write your own js file so that you don't have to use document.write

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top