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)]();
Related
This question already has answers here:
Why do you see colons while calling a javascript function in html sometimes? [duplicate]
(3 answers)
When is the 'javascript:' prefix valid syntax?
(4 answers)
What's the point of "javascript:" in code (not URLs)? [duplicate]
(4 answers)
Closed last year.
In an old piece of legacy code I found the following onclick event:
onclick="javascript:bSubmitted=true;"
Does anyone know what the javascript: label in front of this code does? I have never seen this notation before, and as far as I know whatever is in the onclick event is always javascript. Removing it, or changing it to something else doesn't seem to have an effect and IntelliJ seems to think it's an 'unnecessary label'.
Just out of curiousity I still would like to know what it does and what it can be used for. Does anyone know?
I think you can find your answer here (it's the only "official" source I've found that talks about this): HTML Standard.
Taken from 7.8.1 Navigating across documents
If resource is a request whose url's scheme is "javascript" Queue a
task, on the DOM manipulation task source and associated with the
active document of browsingContext, to run these steps:
Let response be the result of executing a javascript: URL request
given resource, the source browsing context, and browsingContext. Run
process a navigate response with resource, response, navigationType,
the source browsing context, browsingContext,
incumbentNavigationOrigin, and activeDocumentNavigationOrigin.
Basically, the javascript: can be used as an url scheme which (just for adding something to the boilerplate) was and still is used for many XSS injections.
In that case specifically (upon a click event) it sounds unnecessary indeed, while it could be useful on an href, as you may see here (fiddle linked because the StackOverflow snippet manager doesn't allow alerts on javascript:): https://jsfiddle.net/73rmzjgw/
When any changes perform into form then javascript checks that, any changes is there or not. if it has any then it alerts the user before they navigate away from the page.
This action can be disabled on a submit button click by adding
onclick="javascript:bSubmitted=true;"
to its tag.
This question already has answers here:
Console.log is there a way to name sent variables?
(4 answers)
Why can't I get the variable name as a string? I want to create my own 'console.log' function
(3 answers)
Closed 5 years ago.
When I use console.log to debug I typically use a line of code that looks like this.
console.log("totalLength " +totalLength);
My output includes the variable name and its value.
But is there shorthand for this? Or is there maybe a better/faster/more convenient line of code to monitor variables? Thanks so much!
Yes, it's called using the debugger. Printing out variable values is not a good debugging practice.
If you are using Chrome, open up the Developer Tools and go to the Source tab. In the sidebar take a look at "Watch" and add your expressions in it.
You can also just look at source code panel. It will show all the variables' value by default. The "Watch" section allows more flexible expressions.
You can monitor all the variables present in the global scope.
for(var b in window) {
if(window.hasOwnProperty(b)) console.log(b);
}
The window object contains the information about the functions and vars present. You can essentially type window in console and monitor.
This question already has answers here:
What is Facebook's function __d
(3 answers)
Closed 9 years ago.
I've noticed something interesting what Facebook do, and I'd like to know what they are doing and why? If you look at their source code on one of their .js files they seem to be doing a lot of this:
__d("AjaxRequest",["Erro...
__d("FBAjaxRequest",["AjaxRequest"...
__d("CallbackManagerController",...
There seems to be no variables or ordinary functions and objects in any of their JavaScript files. There are functions, but there all arguments passed to this __d method, like this:
__d("keyMirror",[],function(a,b,c,d,e,f){var g=function(h){var i={},j;if(!h)return h;for(j in h){if(!h.hasOwnProperty(j))continue;i[j]=j;}return i;};e.exports=g;});
Is this some sort of optimization for JavaScript, or is it used for easier management?
Thanks
Maybe just a wrapper that does things like dependency injection and error handling. If you can find out where __d is define, you'll know for sure.
It doesn't look like an optimizing (ie. a packer).
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the meaning of "$" sign in javascript
If Jquery is just external javascript code, then how does it use the dollar sign? Wouldn't that be adding new syntax?
$ is just a normal variable. You can do var $ = 42;.
jQuery - and any other javascript framework, simply encapsulate existing functionality in different utility functions. Most platforms endeavor to cover different browser implementations.
So, jQuery's dollar-sign operator is simply a function in which javascript functionality is encapsulated. It is, in essence, an alias for document.getElementById, but it also covers getting by class name or tag name.
As for adding new syntax, in short: no, it does not. The look and organization of code written in a framework may be different, but at the core you're still writing javascript, you're just using a set of functions provided by the library instead of using the set of functions in javascript core.
var $ = function () {
// do something here
}