javascript > variable reset help!

N

Nikita the Spider

Thanks again for the reply.

You're welcome.
It makes sense what you're saying, but I do not believe it to be a
server-side error because the variable is actually assigned. When you
type baseball, it generates a link that says baseball and when you
click the link, it takes you to an RSS baseball search on bloglines. if
you go back and now type "RSS football", the link says football now,
but when you click it, it still takes you to the RSS baseball search.

OK, I think I understand your problem now. If I input "rss baseball",
hit enter, I get back a screen with this added:
Human: rss baseball
botCAD: baseball.

I do the same again with rss football and I get the same screen as my
initial one, but with this added:
Human: rss football
botCAD: football.

Human: rss baseball
botCAD: baseball.

Your problem is that *both* links now go to baseball, is that right? And
if you enter "rss lacrosse" you'll get another screen with three links
that all point to baseball, etc.

If that's your complaint, then the problem is indeed in your Javascript.
Your server-side process that writes the page adds a SCRIPT section like
so (edited for brevity):

<script language="JavaScript"> var name = "baseball";
function Popup(){...}
</script>

So far so good. The problem arises when you have multiple entries in the
page, then you get something like this:

<script language="JavaScript"> var name = "lacrosse";
function Popup(){...}
</script>

<script language="JavaScript"> var name = "football";
function Popup(){...}
</script>

<script language="JavaScript"> var name = "baseball";
function Popup(){...}
</script>

The variable "name" is a global variable. Global variables in JavaScript
exist in all SCRIPT blocks, so something like the code below is valid.
Try it out!

<script language="JavaScript">var foo="A grail-shaped beacon!"</script>

<script language="JavaScript">alert(foo);</script>

Now stop and look at your script with this in mind. Can you now see
where the problem arises?

..
..
..

What's happening is that you set the global variable "name" three times,
and the last time you set it to "baseball", so that's what it is set to
in every single SCRIPT block where it is referenced.

There's a number of ways to solve this; one way would be to change
Popup() to accept a parameter rather than relying on a global variable
and then embed the magic string in the call to Popup() like so:

<a href="javascript:popup('football')"> football</a>.

Better still, avoid the dubious "javascript:" stuff and code the link so
that it will even work for people like me who have JavaScript off by
default (thank you NoScript extension!):

<a href="http://www.bloglines.com/search?q=football"
onclick="Popup('football'); return true;"> football</a>

Last but not least, the function Popup() is repeated three times but it
is the same each time. It should really only be in there once.
any other thoughts?

Yes, please don't top post. I think most people in this group find it
difficult to read. (Not what you meant with your question, I know.)
http://en.wikipedia.org/wiki/Top_posting


HTH
 
C

CADD

Nikita said:
You're welcome.


OK, I think I understand your problem now. If I input "rss baseball",
hit enter, I get back a screen with this added:
Human: rss baseball
botCAD: baseball.

I do the same again with rss football and I get the same screen as my
initial one, but with this added:
Human: rss football
botCAD: football.

Human: rss baseball
botCAD: baseball.

Your problem is that *both* links now go to baseball, is that right? And
if you enter "rss lacrosse" you'll get another screen with three links
that all point to baseball, etc.

If that's your complaint, then the problem is indeed in your Javascript.
Your server-side process that writes the page adds a SCRIPT section like
so (edited for brevity):

<script language="JavaScript"> var name = "baseball";
function Popup(){...}
</script>

So far so good. The problem arises when you have multiple entries in the
page, then you get something like this:

<script language="JavaScript"> var name = "lacrosse";
function Popup(){...}
</script>

<script language="JavaScript"> var name = "football";
function Popup(){...}
</script>

<script language="JavaScript"> var name = "baseball";
function Popup(){...}
</script>

The variable "name" is a global variable. Global variables in JavaScript
exist in all SCRIPT blocks, so something like the code below is valid.
Try it out!

<script language="JavaScript">var foo="A grail-shaped beacon!"</script>

<script language="JavaScript">alert(foo);</script>

Now stop and look at your script with this in mind. Can you now see
where the problem arises?

.
.
.

What's happening is that you set the global variable "name" three times,
and the last time you set it to "baseball", so that's what it is set to
in every single SCRIPT block where it is referenced.

There's a number of ways to solve this; one way would be to change
Popup() to accept a parameter rather than relying on a global variable
and then embed the magic string in the call to Popup() like so:

<a href="javascript:popup('football')"> football</a>.

Better still, avoid the dubious "javascript:" stuff and code the link so
that it will even work for people like me who have JavaScript off by
default (thank you NoScript extension!):

<a href="http://www.bloglines.com/search?q=football"
onclick="Popup('football'); return true;"> football</a>

Last but not least, the function Popup() is repeated three times but it
is the same each time. It should really only be in there once.


Yes, please don't top post. I think most people in this group find it
difficult to read. (Not what you meant with your question, I know.)
http://en.wikipedia.org/wiki/Top_posting


HTH


A problem with the javascript sounds more logical. :)

Thank you for taking the time to see my issue.

I was able to resolve my issue by replacing the previous code with
this:

<script language="JavaScript">
var str="<person/>"
document.write((str.link("http://www.bloglines.com/search?q=<person/>"))
</script>

the only problem now is getting it to open in a new window.

Any ideas?
 
N

Nikita the Spider

Thank you for taking the time to see my issue.

Thank you for bottom posting.

I was able to resolve my issue by replacing the previous code with
this:

<script language="JavaScript">
var str="<person/>"
document.write((str.link("http://www.bloglines.com/search?q=<person/>"))
</script>

the only problem now is getting it to open in a new window.

Any ideas?

You're back into AIML land again, about which I know nothing. But
experimenting with Javascript and checking out some example online out
to get you to where you want to be. And if all else fails, there's
always comp.lang.javascript.

Good luck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top