0

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

0

5 Answers 5

2

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 <).

Sign up to request clarification or add additional context in comments.

1 Comment

This is true. I didn't catch this.
0

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

Comments

0

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>

1 Comment

It would error on $ being undefined before it even got to the attempt to call readjson is that was the problem.
0

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>

1 Comment

No. The load event would not trigger until the other script was loaded.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.