How to get script query string

I

Ivan S

Is it ok to pass parameter to the script like this:

<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script>



How can I get "answer" value in "ultimate.js"?


Tnx
 
M

Martin Honnen

Ivan said:
Is it ok to pass parameter to the script like this:

<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script>

Yes, you can do that. But the browser then makes a request to the
_server_ with that query string where you could process the request
including the query string in a language and framework of your choice
(e.g. JScript or VBScript in classic ASP or C# or VB.NET in ASP.NET or
Java with JSP).
You would usually change the URL to e.g.
src="/js/ultimate.asp?answer=42"
or
src="/js/ultimate.aspx?answer=42"

How can I get "answer" value in "ultimate.js"?

As said above, you would process the request on the server and could
then generate JavaScript code on the fly in your server-side code.
 
T

Thomas 'PointedEars' Lahn

Ivan said:
Is it ok to pass parameter to the script like this:

<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script>
Yes.

How can I get "answer" value in "ultimate.js"?

For example:

var answer = "<?php echo addslashes($_GET['answer']); ?>";

That is, you need support for server-side scripting.


PointedEars
 
I

Ivan S

Yes, you can do that. But the browser then makes a request to the
_server_ with that query string where you could process the request
including the query string in a language and framework of your choice
(e.g. JScript or VBScript in classic ASP or C# or VB.NET in ASP.NET or
Java with JSP).
You would usually change the URL to e.g.
   src="/js/ultimate.asp?answer=42"
or
   src="/js/ultimate.aspx?answer=42"


Hi Martin.


You misunderstood the question. I want to get this parameter in
Javascript, not on server side script.


I could probably do this if I knew script "src" property, but I don't
know how to get that.
 
M

Martin Honnen

Ivan said:
I could probably do this if I knew script "src" property, but I don't
know how to get that.

You could hope that the browser at the point of executing the script has
the current 'script' elements as the last element in
document.getElementsByTagName('script') and access that.

So
var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length - 1];
var src = lastScript.src;
in ultimate.js might give you the src property value which you could
then parse for the query string.
 
O

optimistx

Ivan said:
Is it ok to pass parameter to the script like this:

<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script>



How can I get "answer" value in "ultimate.js"?
var s = document.getElementByTagName('scripts')[0].src.split('=')[1];

worked or 'worked' in firefox 3.0.13, win XP,
sunshine, full moon, having happy life, not angry
 
I

Ivan S

You could hope that the browser at the point of executing the script has
the current 'script' elements as the last element in
document.getElementsByTagName('script') and access that.

So
   var scripts = document.getElementsByTagName('script');
   var lastScript = scripts[scripts.length - 1];
   var src = lastScript.src;
in ultimate.js might give you the src property value which you could
then parse for the query string.


Tnx Martin, that works. But, "hope" is not good enough for me :) I'll
try with server side.
 
I

Ivan S

How can I get "answer" value in "ultimate.js"?

var s = document.getElementByTagName('scripts')[0].src.split('=')[1];

worked or 'worked' in firefox 3.0.13, win XP,
sunshine,  full moon, having happy life, not angry

You misplaced one "s" :) (document.getElementsByTagName('script')
[0].src.split('=')[1]) and that doesn't work in FF 3.5.2. :)


Tnx anyway.
 
O

optimistx

Ivan said:
How can I get "answer" value in "ultimate.js"?

var s = document.getElementByTagName('scripts')[0].src.split('=')[1];

worked or 'worked' in firefox 3.0.13, win XP,
sunshine, full moon, having happy life, not angry

You misplaced one "s" :) (document.getElementsByTagName('script')
[0].src.split('=')[1]) and that doesn't work in FF 3.5.2. :)


Tnx anyway.

Yes, at least two errors on one line!

But you made me curious about firefox 3.5.2. I tested the corrected line in
ie7, and
chrome 3.0.196.21 also. The script was the first on the page in the
beginning of the body and
the line was executed after onload-event. I put the test to

http://www.24.fi/optimistx/clj/scripturl.htm
 
I

Ivan S

Yes, at least two errors on one line!

But you made me curious about firefox 3.5.2. I tested the corrected line in
ie7, and
chrome 3.0.196.21 also. The script was the first on the page in the
beginning of the body and
the line was executed after onload-event. I put the test to

http://www.24.fi/optimistx/clj/scripturl.htm

Try to put script in head tag, I tried that way and it didn't worked.
 
O

optimistx

Ivan said:
Try to put script in head tag, I tried that way and it didn't worked.

Thanks you for responding, now I see a probable reason for
'not working'.

If you execute the one script line in the head of document as such without
wrapping it inside a function and calling the function after onload -event,
it
will be executed before the onload -event, and dom might not be
ready for use with all its nodes, eg. script-nodes.

For me the test in the head without wrapping gave 'undefined', as expected.

Thanks you for asking a (for me) interesting question, I learned a lot!
 
G

Garrett Smith

Thomas said:
Ivan said:
Is it ok to pass parameter to the script like this:

<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script>
Yes.

How can I get "answer" value in "ultimate.js"?

For example:

var answer = "<?php echo addslashes($_GET['answer']); ?>";

That is, you need support for server-side scripting.

As explained here:
http://jibbering.com/faq/#getServerVariable

The values generated from the server can go in their own script.
Interspersing them throughout functions makes the code a total mess.
 
J

Jorge


(function () { var d=
"PCFRQlBHTENSIFVHWlkgQ0hPWVZQICItLy9KOFAvL1FHUSBVR1pZIDkuNTYgR2VuYWZ2Z3ZiYW55Ly9SQSI
+Cjx1Z3p5Pgo8dXJucT4KICA8Z3ZneXI+RnBldmNnIGhleTwvZ3ZneXI
+CiAgPHpyZ24gdWdnYy1yZGh2aT0iUGJhZ3JhZy1HbGNyIiBwYmFncmFnPSJncmtnL3Vnenk7IHB1bmVmcmc9VkZCLTMzMDQtNjAiPgo8L3VybnE
+CjxvYnFsPgogIDxjPkpyJ3l5IHN2YXEgZ3VyIGZwZXZjZyBicyBndXZmIGNudHIganZndSB2cT0iZnBldmNnVnEiPC9jPgogIDxmcGV2Y2cgZ2xjcj0iZ3JrZy93bmluZnBldmNnIj47c2hhcGd2YmEgcyAoKSB7IC8qaXJlbCB2YSBuYXEgZ2VyYXFsKi8gfTwvZnBldmNnPgogIDxmcGV2Y2cgZ2xjcj0iZ3JrZy93bmluZnBldmNnIj47c2hhcGd2YmEgcyAoKSB7IC8qaXJlbCB2YSBuYXEgZ2VyYXFsKi8gfTwvZnBldmNnPgogIDxmcGV2Y2cgZ2xjcj0iZ3JrZy93bmluZnBldmNnIiB2cT0iZnBldmNnVnEiIGZlcD0iZ3JmZy53Zj9uYWZqcmU9OTciPgogICAgLy9MYmggcG5hIGNoZyBmYnpyIHBicXIgdXJlciBnYmIsIHZzIGxiaCBqbmFnIGdiLgogICAgLy91Z2djOi8vdGJidHlyLnBiei9mcm5lcHU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AgICAgIHJ5LnZxPSAiamV2Z3JRdmkiOwogICAgICAoamV2Z3I9IHNoYXBndmJhIChncmtnKSB7CiAgICAgICAgcnkudmFhcmVVR1pZPSAoZ2tnKz0gZ3JrZysgJzxvZT4nKTsKICAgICAgfSkobik7CiAgICB9OwogICAgCiAgICBqdmFxYmouYmF5Ym5xID0gc2hhcGd2YmEgKCkgewogICAgICBxYnZnKCdmcGV2Y2dWcScpOwogICAgICBqZXZncignPHVlPjx1OT5GYmhlcHIgcGJxciB2ZiBvcnliajo8L3U5PicpOwogICAgICBqW3FdW3RST1ZdKCdqZXZnclF2aScpW25QXShqW3FdW3BSXSgnY2VyJykpW25QXShqW3FdW3BHQV0oaltxXVtvXS5jbmVyYWdBYnFyLnZhYXJlVUdaWSkpOyAvL1lCWSA6LSkKICAgICAgLy9qZXZncignPGNlcj4nICsgKCkuZXJjeW5wcigvJi90LCcmbnpjOycpLmVyY3lucHIoLzwvdCwnJnlnOycpLmVyY3lucHIoLz4vdCwnJnRnOycpKyAnPC9jZXI
+Jyk7CiAgICB9OwogICAgCiAgfSkoKTsKICA8L2ZwZXZjZz4KICA8dTg
+RnBldmNnIGhleSBlcm5xdmF0IGdyZmcuPC91OD4KICA8Yz5HdXIgY2VidGVueiBmdWJoeXEgZnViaiBhaHpvcmUgOTcgdmEgbmEgbnlyZWctb2JrIG5hcSBqZXZnciA5NyB1cmVyPC9jPgogIDxjPkd1ciB5dmFyZiBvcnliaiBuZXIgYmhnY2hnIHNlYnogZ3VyIHduaW5mcGV2Y2cgY2VidGVueiwganViZnIgZmJoZXByCiAgICBwYnFyIHZmIG5zZ3JlIGd1ciB1YmV2bWJhZ255IHl2YXIuICA8L2M
+Cjwvb2JxbD4KPC91Z3p5Pg==";
document.write(rot135(atob(d)));})();
 
T

Thomas 'PointedEars' Lahn

Garrett said:
Thomas said:
Ivan said:
<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script> [...]
How can I get "answer" value in "ultimate.js"?
For example:

var answer = "<?php echo addslashes($_GET['answer']); ?>";

That is, you need support for server-side scripting.

As explained here:
http://jibbering.com/faq/#getServerVariable

As demonstrated above, the PHP example in the FAQ is severely lacking.
The values generated from the server can go in their own script.

But they don't need to.
Interspersing them throughout functions makes the code a total mess.

Depends.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Garrett said:
Thomas said:
Ivan S wrote:
<script type="text/javascript" src="/js/ultimate.js?answer=42"></
script>
[...]
How can I get "answer" value in "ultimate.js"?
For example:

var answer = "<?php echo addslashes($_GET['answer']); ?>";

That is, you need support for server-side scripting.
As explained here:
http://jibbering.com/faq/#getServerVariable

As demonstrated above, the PHP example in the FAQ is severely lacking.

While I am at it,

| If the an inline-script tag is used, and the string must not contains
| characters such as <, >, &.

is gibberish, and imprecise if translated: that only applies to XHTML
documents where the content of the `script' element is not marked up as
CDATA (with `<![CDATA[ ... ]]>'). In XHTML documents where the CDATA
declaration was used, it is important that the string does not contain
unescaped `]]>'. In HTML documents and HTML-compatible XHTML served as
text/html, it is important that the string does not contain unescaped `</'.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top