|
Posted by Nikita the Spider on 08/31/06 20:13
In article <1157039487.832462.265050@74g2000cwt.googlegroups.com>,
"CADD" <caddcreativity@gmail.com> wrote:
> Nikita the Spider wrote:
> > In article <1156971832.130118.220850@i42g2000cwa.googlegroups.com>,
> > "CADD" <caddcreativity@gmail.com> wrote:
> > >
> > > Yes, the user submits his input by pressing a button in an HTML form -
> > > you can give it a try here:
> > > http://www.pandorabots.com/pandora/talk?botid=875d96ffae366536
> > >
> > > It's definitely something with the javascript example, the variable is
> > > definitely making it through the script, but just not leaving when it's
> > > the end of the function. It was my first attempt of putting together
> > > javascript used for anything like that, so i can't say for sure that my
> > > use of variables was appropriate.
> >
> > Having looked at the example, I'm not sure what the problem is. I go to
> > the URL that you gave above, type in "rss baseball", hit enter, and I
> > get another screen that looks basically the same but with this added:
> > Human: rss baseball
> > botCAD: baseball.
> >
> > On that page, it looks like the very top of the <script> block (var name
> > = " baseball";) was generated by a server-side process based on my
> > input. If the server-side process isn't writing the correct value into
> > that variable, then this isn't a Javascript problem and it isn't an HTML
> > problem, it is a problem with your server side code. Make sense?
>
> 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
--
Philip
http://NikitaTheSpider.com/
Whole-site HTML validation, link checking and more
Navigation:
[Reply to this message]
|