Accessing variables in another script - javascript

I have a JS
function check(obj) {
for( i=0; i<obj.elements.length; i++){
if(obj.elements[i].type=="checkbox"&&obj.elements[i].checked){
if(confirm(onSubmitMessage)){
return true;
}
else{
return false;
}
}
}
alert(alertMessage);
return false;
}
It's called from jsp page like this:
<script src="/TestAppChanged/check.js" type="text/javascript">
var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
...
<form action="MyAction" method="POST"
onsubmit="return check(this)">
The problem is in that it doesn't see these glabal variables: onSubmitMessage and alertMessage. I thought that the problem was in the way that these things are set and changed it's values to usual strings like "qwe" but it didn't work again. So script in it's body simply doesn't see these variables. And the question is how to get them from script?

The <script> tag is used either to load an external file, like <script src="path/to/file.js"></script> or to define JS code inside the tag, like:
<script type="text/javascript">
// Your JS code.
</script>
You can't have both on the same tag. Thus, split your code:
<script src="/TestAppChanged/check.js" type="text/javascript"></script>
<script type="text/javascript">
var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>

A block between <script src=""></script> parsed when the browser don't support javascript. You need another <script> block, and put in this your variables.

It doesn't see the globar variabes because you declare a source in the script tag.
You should remove the global variables and declare them before the code of the function, in the js page, and then simply include the page like this:
<script src="/TestAppChanged/check.js" type="text/javascript"></script>

You should have two script tags. One for the variables. And then another one below it, linking to your javascript file.

You can set contents in tag OR set the src attribute.
It should be:
<script type="text/javascript">
var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
<script src="/TestAppChanged/check.js" type="text/javascript"></script>

Don't mix the src attribute and code within the tag. Use two tags.

Related

Access object from script tag

I'm writing java script code in script tag in head like:
<head>
<script language="javascript">
object o = new object({....});
</script>
</head>
and trying to use object a in body tag
<body>
<script>
alert(o.value);
</script>
</body>
how can i access object from body???
is their any alternatives?
<head>
<script language="javascript">
var o = new Object();
o.value="a"
</script>
</head>
<body>
<script>
$(document).ready(function()
{
alert(o.value)
});
</script>
</body>
In this case var a is accessible in complete application, but one thing you need to make sure if you are using external JS files then it must be loaded when you using the variable. try onload function to assure JS is loaded and ready to use in body:
window.onload = function ()
{
alert(a);
}
Since your variable is declared outside any functions it can be accessed from anywhere in your document from the same script block or from a seperate script block like in your example. it can even be accessed from html event-attributes like this (note that its better to attach events to html elements using using js):
<button onclick="alert(a);">Click this button to open an alert!</button>

Access Session variables in JavaScript

I wanted to access a session variable in javascript in asp.net mvc application. I have found a way to do it in aspx view engine but not in razor.
Please tell me a way to access the session variables
You can do it this way for a String variable:
<script type="text/javascript">
var someSessionVariable = '#Session["SomeSessionVariable"]';
</script>
Or like this if it's numeric:
<script type="text/javascript">
var someSessionVariable = #Session["SomeSessionVariable"];
</script>
This is really not a very clean approach though, and requires inline JavaScript rather than using script files. Be careful not to get carried away with this.
I personally like the data attribute pattern.
In your Razor code:
<div id="myDiv" data-value="#Request.RequestContext.HttpContext.Session["someKey"]"></div>
In your javascript:
var value = $("#myDiv").data('value');
In my asp.net I am not getting the result by
<script type="text/javascript">
var someSessionVariable = '#Session["SomeSessionVariable"]';
</script>
But I get the answer by below code,
<script type="text/javascript">
var yourVariable = '<%= Session["SessionKey"] %>';
</script>
For google searchers,
In addition, If you want to access the session variable in external .js file you can simply do like this,
------ SOME HTML PAGE ------
//Scripts below Html page
<script>
//Variable you want to access
var mySessionVariable = '#Session["mySessionVariable"]';
</script>
// Load External Javascript file
<script src="~/scripts/MyScripts/NewFile.js"></script>
Inside NewFile.js
$(document).ready(function () {
alert(mySessionVariable);
});

Include external JS file and setting variable

I need to include an external JS-file with
<script src="/IframeHelper.js" type="text/javascript"></script>
But I need to set a variable and I tried somthing like this:
<script src="/IframeHelper.js" type="text/javascript">var $Vari = 'Para';</script>
And inside the "IFrameHelper.js" something like this:
var $Site = 'Rootfolder' + $Vari;
But you know...this doesn´t work. But how can I do this? It´s a little bit like calling a script with parameters. But such a parameter can be a JSON-String oder "long text".
As per my understanding you need to include IframeHelper.js file after you declare your variable.
<script type="text/javascript">var $Vari = 'Para';</script>
<script src="/IframeHelper.js" type="text/javascript"></script>

How to set a variable inside script tag

I have a page name index.php. And I have a script variable as follows at the top of the page:
<script>
var search_quarry = "some_test";
</script>
At the bottom of the same page I want to add this variable into the src attribute of a script tag:
<script src=NEED_TO_ADD_HERE></script>
This doesn't work:
<script src=search_quarry> </script>
Can anyone please tell me how to do this?
You'd need to do with DOM manipulation:
var search_query = 'some_test';
s = document.createElement('script');
s.src = search_query;
document.getElementsByTagName('head')[0].appendChild(s);
or, as an alternative, though I'm not sure if this'd work:
<script id="fixme"></script>
<script type="text/javascript">
var search_query = 'some_test';
document.getElementById('fixme').src = search_query;
</script>
Why would you do this? Seems like a hack to make sever code work without fixing the back end
Option 1 is with document.write
<script>
document.write("<script src='" + search_quarry + "'></scr" + "ipt>");
</script>
Option 2 is to use createElement and appendChild
<script>
var scr = document.createElement("script");
scr.src = search_quarry;
document.getElementsByTagName("head")[0].appendChild(scr); //or use body with document onready
</script>
This is not possible. You cannot use a javascript variable inside the declaration tags for javascript. That is the point of the tags: everything inside them is parsed as javascript; everything outside them is not.
Here's a simple way to do what you want
window.onload=function(){
var script=document.createElement("script");
script.type="text/javascript";
script.src=search_quarry;
document.appendChild(script)
}
Although you would like to change window.onload into something else or just put the code inside at the top level, or if using jQuery:
$(document).ready(function(){
$(document).append("<script type='text/javascript' src='"+search_quarry+"'/>");
});

Why will an object literal load when in declared in external source file but not when called in a file including an external javascript file file

The code in the external file is
var testing = {
bugtest: function() {
alert('No Bugs Here');
}
}
In the php file I am using
<script type="text/javascript" src="externalScript.js">
testing.bugtest();
</script>
But this will not work why?
if I call the function in the external fil it works
var testing = {
bugtest: function() {
alert('No Bugs Here');
}
}
testing.bugtest()
this will work but this not what I want it to do I want to be able to call the function in the main file? What would the cause of this problem be?
You can not use src attribute and the text node with script elements.
They must be exclusive, e.g. an element each.
So your HTML would look something like...
<script type="text/javascript" src="externalScript.js"></script>
<script type="text/javascript">
testing.bugtest();
</script>
This
<script type="text/javascript" src="externalScript.js">
testing.bugtest();
</script>
is wrong. You can either specify a src, or run inline code.

Categories