src="xyz.js?id=123"

E

|-|erc

Hi,

can a .js file read a parameter into it from the HTML file?

this is the code to use in peoples HTML files, each member has their own id.

<!-- TopCounters Code START -->
<script language="JavaScript" src="http://www.topcounters.com/pphlogger.js?id=johnsmith"> </script>


the original pphlogger.js has this in it

/* -----------------------------------------------
POWER PHLOGGER - v.2.2.5
// ----------------------------------------------------------
// SETTINGS:
// here should be your username you received from www.topcounters.com
// Do not edit this file manually!! Use the one you got in your
// confirmation-email or the one from PowerPhlogger's settings
// section.
id = "johnsmith";


So I want id to be set from the parameter not in the file.
I thought you could parse the URL bar after the ? but the .js wont go into the URL bar.

Herc
 
M

Mick White

|-|erc said:
Hi,

can a .js file read a parameter into it from the HTML file?

this is the code to use in peoples HTML files, each member has their own id.

<!-- TopCounters Code START -->
<script language="JavaScript" src="http://www.topcounters.com/pphlogger.js?id=johnsmith"> </script>

The js expression "location.search.substring(1)" will yield the
String "id=johnsmith", it's a small step to further parse the
query string.
Mick
 
I

Ivo

|-|erc said:
can a .js file read a parameter into it from the HTML file?
this is the code to use in peoples HTML files, each member has their own id.

<script language="JavaScript"
src="http://www.topcounters.com/pphlogger.js?id=johnsmith"> said:
So I want id to be set from the parameter not in the file.
I thought you could parse the URL bar after the ? but the .js wont go into
the URL bar.

Parameters attached to the src of a js file are really only useful if the js
file is created on the fly by the server and different parameters result in
different files sent to the client. If you want, you can detect the
parameter with a bit of script like this:
var s=document.scripts[0].src;
s=s.substr(s.indexOf('?')+1);
but this doesn't seem very efficient, when you can more easily do:
<script type="text/javascript"> var id='johnsmith'; </script>
<script type="text/javascript" src="blabla.js"></script>

HTH
Ivo
 
L

Lee

Mick White said:
The js expression "location.search.substring(1)" will yield the
String "id=johnsmith", it's a small step to further parse the
query string.

location.search will refer to the query of the current page,
not the value in the URL of the .js file.

The simplest solution would seem to be:

<script type="text/javascript">id="johnsmith"</script>
<script type="text/javascript"
src="http://www.topcounters.com/pphlogger.js?></script>

Since all of the script is executed in the current page, the
code in pphlogger.js can see global variables in this page.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Parameters attached to the src of a js file are really only useful if the js
file is created on the fly by the server and different parameters result in
different files sent to the client.

They can also be used to pass parameters between successive pages on one
site.

Page 1
<br><a href="page3.htm?a>link a</a>
<br><a href="page3.htm?b>link b</a>

Page 2
<br><a href="page3.htm?A>link A</a>
<br><a href="page3.htm?B>link B</a>

Then script in page 3 can determine whether it was called (probably)
from A or a or B or B.


Each of my normal pages, e.g. itself.htm can frame itself by being
clicked on <a href="frames-4.htm?itself.htm">Frame This</a> (even if
already framed!).
 
J

Jimmy Cerra

Oh Strong One,
So I want id to be set from the parameter not in the file.
I thought you could parse the URL bar after the ? but the
.js wont go into the URL bar.

That isn't easy. The best way is to id the script DURING ONLOAD and
extract the url:

<script
id="example"
type="text/javascript"
src="http://www.example.com/script.js?prop1=value1"</script>

..
..
..

window.onload = function() {
var urlForScript = document.getElementById("example").src;
/*
* Parse urlForScript with a Reg Exp
* or other String Function
*/
};

Make sure you don't try to execute that before the page has loaded,
otherwise you might get an error or even "undefined" for src! Hope
that helps.
 
E

|-|erc

Dr John Stockton said:
JRS: In article <[email protected]>, seen in


They can also be used to pass parameters between successive pages on one
site.

Page 1
<br><a href="page3.htm?a>link a</a>
<br><a href="page3.htm?b>link b</a>

Page 2
<br><a href="page3.htm?A>link A</a>
<br><a href="page3.htm?B>link B</a>

Then script in page 3 can determine whether it was called (probably)
from A or a or B or B.


Each of my normal pages, e.g. itself.htm can frame itself by being
clicked on <a href="frames-4.htm?itself.htm">Frame This</a> (even if
already framed!).

that's pretty standard if it goes into the URL bar, but loading .js is done
in the background.

if you want to see some *really* advanced throwing parameters around,
check how I converted a PHP chat script into a live chess tournament server.
www.chessit.com (swap to black if you have to then drag and drop a chessman).

thanks all the topcounters HTML is running now.

Herc
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top