[thelist] [javascript] passing vars in url to window.open

.jeff jeff at members.evolt.org
Thu Oct 17 17:35:01 CDT 2002


paul,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Paul Bennett
>
> OK this is driving me nuts this morning:
> I have a js function:
>
> function viewNote(url)
> {
>   window.open("url", [...]
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

your problem is the way you're trying to reference the first argument of the viewNote() function.  by wrapping it in quotes you're declaring it as a string.  so, the url of the new window is literally "url" instead of the value of the argument, url.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> which is called from  a link in this fashion:
> <a href="#" onclick="viewNote('view_note.php\?note_id=<?
> echo $obj_update_account->id ?>'); return false;">view
> note</a> Pretty straightforward i would think?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

no, not straightforward at all.  ok, well maybe it is to your javascript-enabled users.  but that's a completely useless link to your non-javascript users.  you can make that link accessible without any loss of functionality or behavior for your javascript-enabled users.  you can do this by moving the url to the href attribute and putting "this.href" (without the quotes) as the argument for your viewNote() function call in the onclick.  be sure to include the "return false" (again, without the quotes) immediately after the viewNote() function call in your onclick event handler.  if you want non-javascript users to see the note in a new window then be sure to also add a "target" attribute to the <a> tag.  all the changes together would result in the following:

<a href="view_note.php\?note_id=<? echo $obj_update_account->id ?>"
   onclick="viewNote(this.href); return false"
>view note</a>

read more about why this is a good idea in the following two articles:

Links & JavaScript Living Together in Harmony
http://evolt.org/article/thelist/17/20938/

Forms & JavaScript Living Together in Harmony
http://evolt.org/article/thelist/17/28553/

enjoy,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/




More information about the thelist mailing list