how read HTTP GET variables in javascripts? [duplicate] - javascript

This question already has answers here:
How do I get query string value from script path?
(5 answers)
Closed 8 years ago.
I wanna know is this possible I pass get parameters for a javascript file and use them in codes?
I have this in html:
<script type="text/javascript" src="/javafile.js?q=somtext"></script>
know how I use this "q" parameter in my script codes? is this possible?

You can either:
Generate the JS dynamically with a server side language and read the query string there
Assume that the JS is the last <script> element so far in the DOM and parse document.scripts[document.scripts.length - 1].src
Note that if the script element has been added dynamically (i.e. with JS), it might not be the last script in the DOM.

I think Quentin's suggestion is the answer to your question.
I usually use an alternative way for this, which may also help you:
Make sure that your javascript is written in a library form, and make sure that you have an instantiate method/function inside your javascript that allows you to pass parameters (or better, as an object)
// on dom load:
library.init({ var1: value1, var2: value2});
This allows you also to load your javascript in a different way, and allows for cleaner code.

Or you can use option 3: use a library that has this functionality, e.g. MooTools: http://mootools.net/docs/more/Types/String.QueryString

Related

Symbol behind the .js [duplicate]

This question already has answers here:
Why pass parameters to CSS and JavaScript link files like src="../cnt.js?ver=4.0"?
(9 answers)
Closed 3 months ago.
What is "?_=3.5-SNAPSHOT-Dev" mean after ".js"? I do some research on google but no idea what it mean and what it use for
<script type="text/javascript" src="js/jquery-3.5.1.js?_=3.5-SNAPSHOT-Dev"></script>
It depends on the server that's serving your Javascript. Generally, variables like those on JS files are used for cache busting. I think in your example, 3.5-SNAPSHOT-Dev could be a release tag. If this JS file is now on your own machine, you can safely discard the ? and anything after that. If you're getting this from another server somewhere, seek out documentation about that server to see what they use the _ variable for.

How to avoid this javascript eval()? [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 7 years ago.
I'm trying to rid my code of a couple of evals that I have in my javascript, but I'm not sure of the best way to achieve what I'm trying to do.
I have a "filter form" on a bunch of my pages, with a bit of JS attached to it that reloads parts of the page depending on what the user does.
Different pages require different actions though.
So my solution (that I came up with yearrrrs ago...) was
<form name="callback_loadCalendar">
<form name="callback_loadNames">
Etc.
And this horrible bit of JS (attached to onchange events etc) to then call the relevant function:
if (f.getAttribute('name') && f.getAttribute('name').indexOf('callback') === 0)
eval(f.getAttribute('name').substr(9)+'()');
E.g. that would call loadCalendar() and loadNames() respectively.
What SHOULD I be doing instead?
Thanks!
If the functions are in the global scope, then you can use bracket notation in the global scope to access the function references.
window[f.getAttribute('name').substr(9)]();

Using loadUrl to run javascript loads different page [duplicate]

This question already has answers here:
Android WebView always returns null for javascript getElementById on loadUrl
(3 answers)
Closed 5 years ago.
We have an app that uses a form for oauth, just for dev purposes I want to eliminate typing in the user name and password so I added some code like this for when the page finishes loading:
mWebView.loadUrl("javascript:document.getElementById('UserName').value='" + txtUser.getText()+"'");
But for some reason it doesn't fill in the form it makes a new page and just writes out the value of txtUser instead of filling in the input field? Why and how can I fix this?
If you look at a tool look Squirt it does an anonymous function call. So I am not that versed in javascript, but that anonymous call seems to be the key in that the browser sees it as the current page is making the call itself. So try this roughly:
mWebView.loadUrl("javascript:(function(){document.getElementById('UserName').value='" + txtUser.getText()+"';})()");
I faced a similar issue and since I couldn't find a proper solution, I used the following hack -
Fetch the html source text.
Replace </body> tag with </body><script>YOUR JS CODE</script>
webview.loadDataWithBaseURL(url, editedString, "text/html", "UTF-8", null);
Sorry for the hacky solution, but in case you can't find a solution, this could help :)

How to know when to use $variableName or just variableName? [duplicate]

This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 9 years ago.
So I recently discovered that it is useful to name jQuery variables in a way that starts with "$" sign, so var $progress instead of var progress for example.
What I don't understand is, how do you know when to use this? I mean there is such a thin line between javascript and jQuery (there isn't even a line, jQuery is JavaScript) would I use it on variables that are changed with jQuery or something?
At this moment it seems to me that if you have a jQuery based solution that all variables would end up starting with "$", so I'd like to know when to actually do this so I don't confuse myself and others.
There is no need to use $ at the beginning of variable names and some developers actually hate the practice altogether, though I do personally like it. The common use is to note that the value of that variable is a jQuery object.
A jQuery object is what is created in this process:
var $myElement = $('#some-id');
That notes that $myElement contains a jQuery object and therefore it is safe to use jQuery functions on it. For example, $myElement.hide();
Without jQuery, this is how the same code might look:
var myElement = document.getElementById('some-id');
myElement.style.display = 'none';
In that case, myElement is NOT a jQuery object and so myElement.hide() is an error. This is an example of why someone may prefer to have the $ on the variable name... to note that jQuery functions can be called on it. If someone is using jQuery already, it is standard to get all element references with jQuery anyway, so this actually is redundant.
This is really all about coding standards that you and your team have to decide on. It's the same as debating underscores or pascal casing or any other naming convention that you can come up with.
In short: It comes down to personal preference.

Can I pass a parameter directly to a .js file, and how do I get the value [duplicate]

This question already has answers here:
Passing parameters to JavaScript files
(18 answers)
Pass vars to JavaScript via the SRC attribute
(9 answers)
Closed 8 years ago.
I want to pass a parameter to some javascript using a single line of code, like this:
<script language="JavaScript" src="courselist.js?subj=MATH" type="text/javascript" />
Inside the javascript file, how can I get the value of the parameter "subj"?
Thanks
That's as far only possible by accessing "own" <script> element in the HTML DOM and parse the src attribute.
Long story short, here's a nice article with detailed explanations and code samples: http://feather.elektrum.org/book/src.html
Why not just create the variable in inside a script tag before including the javascript file?
<script type="text/javascript">
var subj = "MATH";
</script>
<script language="JavaScript" src="courselist.js" type="text/javascript"></script>
The only way to get something like this to work is to have the server serving up a dynamically generated javascript file where it has something like this on the server:
if(Parameters["subj"]=="MATH"){
jsfile="var subj='MATH'; "+jsfile;
}...
I don't think the Javascript file would be aware of the parameter that is passed to it. If that address goes to some sort of server-side script (NOT just a static Javascript file), then you may be able to do something with it.
When a script, loaded from a script src file is interpreted, its related script element exists in the document.
Don't worry about which file is which- look at every script element for an url with a query string.

Categories

Resources