{literal}
<SCRIPT LANGUAGE="JavaScript"
SRC="../calendar/weeklycalendar.js">
</script>
<script>
// call the function to build the calendar
// function's param specify the first day of week
// 0=Sunday, 1 = Monday, ..., 6=Saturday
alert("before");
buildWeeklyCalendar(1);
alert("afetr");
</script>
{/literal}
this script runs fine on server but when i use it in smarty template it doesn't work.
Can anyone explain.
Thanks
I figured it out. The js file will be called with reference to the PHP file that calls the template . The path will not be according to where the smart y template is but according to where the calling php file is.
Thanks
Related
So I declared a variable empty in external JavaScript file which I am sourcing in my main HTML page and because I want to load that variable from PHP I am doing this but it doesn't seem to work.
<script src="./assets/js/script.js"></script>
<script type="text/javascript">var APIKey = <?php echo $API; ?></script>
script.js is the one having empty global variable like this:
var APIKey = "";
I have already declared $API in PHP and I know it's working because I tried echoing it as text and it works but for some reason it doesn't in script. Please help.
Thanks!
So it took a bit time to figure out but the mistake was pretty simple.
All I had to do was REMOVE this from script.js
var APIKey = "";
The problem was that I was defining it 2 times and I don't think that's supported for some reason but that worked for me so if anyone else has the same issue, it'll work for you as well. Have a good day and keep on coding.
I have the following script tag in my JSP file:
<script src="/js/CCTUtil.js"></script>
with the following function in it:
function disableButton(buttonID) {
document.getElementById(buttonID).setAttribute("disabled", "true");
return true;
}
and in my jsp I call it with:
onchange="disableButton('datasourceForm:cancel');
datasourceForm:cancel is just the ID, so don't worry about that.
This works if I hardcode the JS function in my JSP, but when exporting it to a file, it doesn't work. It recognizes the valid filepath (otherwise the server throws an exception) so it can see the file just fine, but when testing it in Internet Explorer the error is "Object expected", and points to the end of the JSP file, which of course isn't telling of anything.
Help please?
The SRC must not be correct then. Are you sure you have set the path correctly? It's not supposed to be "../js/CCTUtil.js" is it?
Instead of including script file, directly add javascript function in the jsp file.
Then try, if you are getting the same issue, might be some issue with javascript or ur id datasourceForm:cancel
Can anyone please guide me to read the value of Session timeout value using javascript contained in a .js file.
I need to access the value in a javascript file and process the value.
I tried to follow the below approach in a test.js file but it is not working:
var sessionTimeout = <%= Session.Timeout %>;
alert(sessionTimeout);
Your problem is that .JS files are not executed so they do not contain the Session.Timeout variable.
You can do two things:
Include your javascript directly in your ASP/ASPX page that does have the code being executed.
Register your JS script in your code.
Registering your JS
See: http://msdn.microsoft.com/en-us/library/aa479011.aspx#aspnet-usingjavascript_topic07
Page.RegisterClientScriptBlock("MyScript", _
"<script language=javascript src='MyJavaScriptFile.js'>")
I have share m code which I use for connection string from this code you can replace logic for session.
<script language="javascript" type ="text/javascript">
function ReadWebConfig()
{
var strCon = '<%=ConfigurationManager.ConnectionStrings["MysqlCon"].ConnectionString %>'
alert(strCon);
var strTemp = '<%=ConfigurationManager.AppSettings["WordTemplate"].ToString() %>'
alert(strTemp);
}
</script>
I am writing a third party javascript (my.js) that can be inserted in a HTML page using script tag. I want to achieve the following:
my.js gets loaded (which has a function myFunc(params))
myFunc() gets called with appropriate params (parameters can change)
putting my.js script in head is not an option
What is the best approach that I can use?
The problem is that you can't really pass parameters w/ just 1 script tag pointing to an external file, so you would have to get them from some element in the DOM:
The html:
<html>
<body>
<script src="my.js"></script>
<input id="params" type="hidden" value="'param1', 'param2', 'param3'" />
<div id="result"></div>
</body>
</html>
The javascript:
function myfunc() {
var doc = document,
params = doc.getElementById("params").value.split(","); // make an array of params
doc.getElementById("result").innerHTML = params.toString();
}
window.onload = myfunc;
Honestly though, this is a kludge. As mentioned before by Felix, you should probably just use 2 script tags -- One to get the external js file and one to call the function with the parameters you need.
You can pass parameters in via the query string and parse them out dynamically.
For example, your script tag becomes:
<script src="my.js?foo=bar"></script>
You can then get the value of the URL using:
var scripts = document.getElementsByTagName('script');
var url = scripts[ scripts.length - 1 ].getAttribute('src');
Because of the order JS is loaded by the browser, the last script on the page (while your script is executing during load) should always be your script.
Then you parse the query string. There are a bunch of questions on Stack Overflow dealing with that. Ex:
Parse query string in JavaScript
code :
<script type="text/javascript" src="http://127.0.0.1/Test.js#username=stackoverflow">
</script>
iwant to know ,how to get the username in Test.js
file
Test.js :
var username = ??
///////////// #username=stackoverflow
thanks advance
If you are trying to do all this on the client side, it's much better to use:
<script type="text/javascript">//<![CDATA[
var username = "stackoverflow";
//]]></script>
<script type="text/javascript" src="http://127.0.0.1/Test.js"></script>
That way, you don't need to tackle the issue of reading the src attribute of the script tag somehow.
The query portion of the URL is invalid. It should be:
http://127.0.0.1/Test.js?username=stackoverflow
The # is treated as a named anchor.
The gup function isn't good because the parameter is on the script tag, not the HTML output page.
The location object (location.href, location.search...) refers to the HTML page where the script included.
There are 2 other options:
Use this
Use #idealmachine answer. You can wrap the global variable with simple object in order to avoid conflict with other global JS variables