Setting a variable of a javascript from an other, in javascript - javascript

I have the following problem:
In file (let a.js be) I have:
var kindofdisplay ;
In an other file ( let b.js be)
I get the information to set kindofdisplay.
Now, I would like to set kindofdisplay from file b.js so that when a.js is executed it will be able to process the variable in a correct way.
Many thanks

As Raja pointed out. If you can access the kindofdisplay variable on b.js, you can change it. You just need to take care not to declare it again.
You can try give a default value like:
var kindofdisplay='none';
And check if that's the value the variable has when on b.js. If it's not, you are probably declaring it again.

If you're using the two javascript files on different web pages, you could always set the variable as a cookie (providing you don't need it to be secure).
Have a look at this tutorial about cookies.
Another way to do this would be to put the script that defines the function for setting the variable in one file, link it to both pages that you need the variable to exist in and call the function on each page.
Of course, as a couple of people have already explained, if you're using the two javascript files on the same page, there's no need to do this - just ensure that the variable has the appropriate scope.

Related

Access a variable from a jsp file

I have created two jsp pages. I passed a list from jsp page and displayed the contents of the list in the second page using this: ${user.rate},${user.location}etc..
Now, I want to store one of these elements in a local variable for performing some arithmetic operations. I don't know how to save the variables locally and use it.
Kindly help me.
I found the answer. I used c tags to store my values.
<c: set var=name_of_variable value=${user.location} />
And I can access the variable using the 'name_of_variable'.

How to pass a javascript variable value to bash script

I have a problem at work that looks like I'll need to use both some javascript and a shell script to solve.
What I need to know is - how can I pass the value of a javascript variable to a shell script?
My particular use-case is getting the selected value from an HTML dropdown list. That's fine - I can get that value and assign it to a Javascript variable. I now just need to know how to take the next step and get that variable into a bash variable.
So, let's say I have this bit of Javascript -
var foo = "some text" ;
How do I get "foo" into bash?
The answer is you can't with a regular web page. Browser Javascript code is not allowed to effect the local computer's environment for security reasons.
This would take some sort of browser add-on the end-user installed running at higher privileges in order to set an environment variable in the local computer's environment.
You can Uses Global Variable
//base.js
var foo;
And Child Js
foo="some text"
Must Be Load Js file Base First After Second
You can debug Statement usins
console.log(foo);

Javascript .js File Guidelines - How do I use a function outside of this file?

So two part question here. Basically, what is the proper practise for javascript function locations? I assumed it would be to have several MyScriptFile.js files, each with a few functions instead of one huge AllMyScripts.js file, as not every page needs every function.
However I'm not sure how to reference another function from outside of this file...
My situation: I'm using an AJAX request in many of my pages. Each request response is different (drawn from different files, etc) and is very hard to make dynamic (one-script-fits-all would be difficult). However, I do have my MakeAJAXRequest() function which creates the request object, which is standard to all DoSomethingWithRequest() functions.
How do I include MakeAJAXRequest() in the other files which contain the DoSomethingWithRequest() functions? It seems as though I should have been able to find this.. but I havn't come across it.
tl;dr I have MakeObject.js and UseObject.js. How does UseObject() reference MakeObject()?
EDIT: Found that if you include MakeObject BEFORE UseObject in your HTML <script> tags, UseObject will be able to reference MakeObject. Seems a little dirty still, as anybody who wants to use the UseObject script will have to be aware of the MakeObject dependency...
If you want to ensure your dependencies are loaded, you could use a function such as this: http://phpjs.org/functions/include:433 There is also include_once(), require(), and require_once()

less.js: Use custom import function

less.js is using an internal xhr() function to load #imported .less files dynamically via ajax.
I want to know if there is anything I can do the hand a custom function over to the less parser to get the imported files loaded through this function and NOT through the default loading function.
As a wild example: I may have stored the .less file I want to import in my localStorage and want less.js to load it from there, instead via AJAX.
You could always create a fork and override or change loadStyleSheets and have it check in localStorage first.
Since the entire system is designed to run a closure it'll be hard (if not impossible -- I've never tried to modify a closure from outside the closure) to make any additions post-library load.

JavaScript variable scoping - persisting state?

I have the following setup, and I need to know how to persist state.
1.) An external web page uses ajax to load and display a .jsp file, which contains javascript.
2.) Once this rendering is complete, javascript in the .jsp file must be called to perform an action based on the results that occurred during rendering. Specifically, the document does action on $(document).ready, and those actions dictate what must be done on later function calls.
The question I have is this: How do I persist the data and state created when the page is loaded? I tried something like this:
External:
ajax to test.jsp
Internal test.jsp
var saveMe = {};
function getsCalled()
{
saveMe = {'a':function(){return false;}};
}
function needsData()
{
//???
}
Later...
External:
needsData();
Nothing I seem to attempt is working. What would be the best way to persist state in this situation?
If you want to know about scoping read this. It might help you to work out what is going on.
Have you tried declaring saveMe outside of the $(document).ready? Then you should be able to change the value from inside the $(document).ready as well as from the external script. I'm not sure how the scoping works for javascript variables from an ajax call though, so I'm not sure if this would actually work.
Making the variable a member of the function object worked swimmingly.

Categories

Resources