[thelist] Accessing "URL" variables in JavaScript

Aylard JA (James) jaylard at equilon.com
Thu Oct 12 17:54:26 CDT 2000


Skadar,

> My problems lies with the line "location.search.substring(1);".  This, of
> course, refers to the URL.  How can I, instead, refer to the URL within
the
> src attribute of my <script> tag?  Any clues?

	You can do this in IE4+, either by assigning an ID to your <script>
element, or grabbing it from the scripts collection, and referencing its src
attribute, e.g.:

<script language="javascript" type="text/javascript" 
   id="MyScript" 
   src="http://www.myserver.com/myscript.js?ID=%24%29H%2D%28S%40%20%2E%B2">
</script>

document.scripts.MyScript.src ; OR
document.scripts[0].src ;

	In Netscape, I don't think this is possible. Could you, instead, set
up script tags containing a variable for the id, such as:

<script language="javascript" type="text/javascript">
   var strId = "%24%29H%2D%28S%40%20%2E%B2" ;
</script>

hth,
James Aylard

> -----Original Message-----
> From: Steven Wood [mailto:steve.wood at serviceengine.com]
> Sent: October 12, 2000 3:09 PM
> To: Thelist at Lists. Evolt. Org
> Subject: [thelist] Accessing "URL" variables in JavaScript
> 
> 
> This is going to be tough to explain, but here goes.
> 
> I want my clients to drop this code into their websites:
> <script language='javascript'
> src='http://www.myserver.com/myscript.js?ID=%24%29H%2D%28S%40%
> 20%2E%B2'></sc
> ript>
> 
> The ID variable will be unique to them.
> 
> I'm doing this because the code within myscript.js contains 
> javascript that
> outputs a link to a pop-up window that I don't want my 
> clients modifying.  I
> can't figure out how to reference the URL variable 'ID' within the src
> attribute in the <script> tag.  I'm having trouble because 
> it's not really
> IN the URL, if you follow me!
> 
> In myscript.js, I have some javascript that searches the URL 
> and returns the
> value of the ID variable that was passed in it, but I need to 
> know how to
> reference the src attribute above... instead of referencing 
> the actual page
> URL.
> 
> Here is the code within myscript.js:
> 
> var query = location.search.substring(1);
> var pos = query.indexOf('=');
> var ID = query.substring(pos+1,query.length);
> 
> var link = "<a href=\"#\"
> onclick=\"window.open(\'http://www.mywebsite.com/popuppage.cfm
> ?ID=" + ID +
> "\', \'PopupWindow\',
> \'toolbar=no,location=no,directories=no,status=yes,menubar=no,
> scrollbars=yes
> ,resizable=yes,copyhistory=no,width=400,height=500,screenX=150
> ,screenY=100,l
> eft=150,top=100\')\">Pop Up This Window!</a>";
> 
> document.write(link);
> 
> 
> Skadar
> 
> 
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt ! 
> 




More information about the thelist mailing list