I have the j-query function test() in (chart.js) another solution file which is added the reference of main projects. Now i want to call the test() method from default.aspx page.
This is my code.
chart.js
function test(){
//for Example
}
default.aspx
<script>
test(); //call chart.js function
</script>
If i call the test method() from default.aspx page it is "undfined". how to call the j-query method which is located in another solution file but the same is added reference to the main project. Anyone could help on this..
Thanks,
Bharathi
you should put reference to tht perticular js file at starting of the page like below :-
<script type="text/javascript" src="chart.js" />
<script>
test(); //call chart.js function
</script>
Try this
<script type="text/javascript" src="chart.js">
$(document).ready(function()
{
test();
});
</script>
It seems like you are using a JS.page and that you are trying to call it with a script.
If you want to have your chart.js called into the default.aspx page do it like this.
<script src="assets/chart.js"></script>
place this in the head sections
In the head section of your aspx page add this:
<script type="text/javascript" src="chart.js" />//add reference of the chart.js file
<script type="text/javascript" language="javascript">
test(); //call chart.js function
</script>
You have to add that js file (chart.js) before your script block, may be in head tags of page. like following
<script type="text/javascript" src="chart.js" />
and Make sure your jQuery file is added even before it. so it should be like following
<script type="text/javascript" src="jQuery.js" />
<script type="text/javascript" src="chart.js" />
<script type="text/javascript">
test(); //call chart.js function
</script>
Related
I am working with ASP.net and JQuery.
In one Aspx page i have many Js pages included.
Now i added one more Js file(say page1.js) and i was calling a function from page1.js to another js file(page2.js) which was also included in the same Aspx page.
When i tried calling the method(Page1Func) from page2.js i was getting undefined error message.
ASPX Page:
<script src="<%= SomeCommonFunc.GetResolvedUrl("~/Page1.js") %>" type="text/javascript"></script>
<script src="<%= SomeCommonFunc.GetResolvedUrl("~/Page2.js") %>" type="text/javascript"></script>
Page1.js
function page1Func(){
//Some code
}
Page2.js
function page2Func(){
//Some code
page1Func(); // giving undefined error message
}
so i did this:
function page2Func(){
var someFunc = Page1Func; //it worked
someFunc();
}
And it worked.I'm not sure why this message was coming.i have also tried to change the order to include of Js file in ASPX page.
it would be great if someone can help me in explaining this behavior.
P.S : I have checked on internet and didn't get the proper answer for it.
Thanks.
The function could be called as if it was in the same JS File as long as the file containing the definition of the function has being loaded before the first use of the function.
I.e.
File1.js
function alertNumber(number) {
alert(number);
}
File2.js
function alertOne() {
alertNumber("one");
}
HTML
<head>
....
<script src="File1.js" type="text/javascript"></script>
<script src="File2.js" type="text/javascript"></script>
....
</head>
<body>
....
<script type="text/javascript">
alertOne();
</script>
....
</body>
If your code is something like this then there is no chance of giving undefined at all
Here is Plunker implementation
HI i am facing some problem while calling function from index.html on load event
it return error readjson is not defined in console.
index.html
<script type="text/javascript">
$(window).load(function () {
readjson('a','s');
});
</script>
main.js file
<script type="text/javascript">
function readjson(val1,val2){
//some code
}
</script>
Can anyone tell me why it is not able to call, i have link main.js file to index.html
JavaScript files shouldn't include any HTML.
Remove <script type="text/javascript"> and </script> from main.js.
You should see an error when that file is loaded (along the lines of Unexpected token <).
Call it like this inside the js file without the "script" tag :
function readjson(val1,val2){
//some code
}
In index.html you need the "script"
And follow the advice given in the comments, always include first the .js file and then the function in the index.html
Did you add jquery link properly??
If not, you should add jquery link for $(window) to be called. such as..
<script src="http://code.jquery.com/jquery-latest.js"></script>
If you actually have two script declarations as you are showing here the the window load will fire first. It then look for your function, which has not been created yet. That's why you are getting undefined.
You could have one script declaration on the page and place your function in the load function.
<script>
$(window).load(function () {
readjson('a','s');
function readjson(val1,val2){
//some code
}
});
</script>
Remove tags from main.js:
<script type="text/javascript"> and </script>
and remember to include jquery and main.js with:
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
<script src="test.js"></script>
I'm trying to call a function in an external file from within a HTML page using the document.ready jquery functionality. Below is the example of code from my HTML, but it does not execute the function with the code I've written.
<script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="_js/script.js">
//<![CDATA[
$(document).ready(function(){
// What do I run here to grab external file function?
extFunction();
});
//]]/>
Example of function from external file:
function extFunction(){
alert("ALERTED!");
};
<script type="text/javascript" src="_js/script.js">
You cannot have a src attribute on a <script> tag and also have JavaScript code inside the tag. Once the src attribute is seen by the browser, it does not execute anything within the tag. Please make two separate tags...
<script type="text/javascript" src="_js/script.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
// What do I run here to grab external file function?
extFunction();
});
//]]/>
</script>
You cannot have body(content) and src for a script element
<script type="text/javascript" src="_js/script.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
// What do I run here to grab external file function?
extFunction();
});
//]]/>
</script>
you want to call function from a php file or js file ??
if you want to call a function of another .js file just include that file in your .js file and if you want to call a php function then use ajax.
this code is put at the top of my asp.net page:
function Test(HtmlDocument)
{
}
How can I execute this javascript function at the end of my page?
Just invoke the function in <script> tags immediately before the </body> tag.
<html>
<head>
<!-- snip -->
</head>
<body>
<!-- snip -->
<script>
Test(document);
</script>
</body>
</html>
Simply put
<script>Test(document);</script>
In the end of your html
The best way to achieve this is using windows.onload. The event can be used to perform some task as soon as the page finishes loading. Here is an example:
<script type="text/javascript">
function my_code(){
alert(" Alert inside my_code function");
}
window.onload=my_code();
</script>
Or you can put the function at the bottom of the code without the function declaration
<script>
Your javascript code here
</script>
The easy way is to drop a script manager on the page (anywhere within the body of the page) then put something like this on the page ...
<script type="text/javascript">
Sys.Application.add_load(function () {
// do your thing
});
</script>
That executes client side when the document has finished loading.
I moved a function from an html page to an included reference file. It stopped working.
The complete contents of the file are:
alert('file included');
function doAlert() {
alert('functions work');
}
In the html page, I have
<html>
<head><title>Page</title></head>
<body>
HTML Template Header
Some Content
ASP.NET MVC Partial View Starts
<script type="text/javascript">
doAlert();
</script>
ASP.NET MVC Partial View ends
HTML Template Footer
<script type="text/javascript" src="/Scripts/wtf.js"></script>
</body>
</html>
The 'file included' alert works, but the 'functions work' does not. What am I doing wrong?
Make sure you include the file before you execute the function.
<script src="file.js" type="text/javascript"></script>
<script type="text/javascript">
doAlert();
</script>
you should call your function(s) in the following way:
window.onload = function() {
doAlert();
// func_1();
// func_2();
// func_3();
// ...
// func_n();
};
See this for more information about the window.onload event.
Code example on jsFiddle