Reputation: 21
I have two files. HTML and .js files. in code.js, I have written jquery code and in HTML file, I am including code.js as following:
<script src="jquery-1.4.2.js"></script>
<script src="jquery.interval.js"></script>
<script src="code.js">
jQuery.noConflict();
var $jcode = jQuery;
</script>
in code.js, I have written following:
jcode(document).ready(function() {
jcode.interval(checkForms, 2000);
});
When I run it, it gives me error as can not read property of interval undefined. I think there is something wrong with my usage of noConflict. Can you please help?
Thanks
Upvotes: 2
Views: 315
Reputation: 6023
You are mixing internal and external JavScript, which is a bad practice in the least and may even fail.
Replace the code in code.js with:
jQuery(document).ready(function() {
jQuery.interval(checkForms, 2000);
});
In your page, write the script part as follows:
<!-- no JavaScript before this line -->
<script src="jquery-1.4.2.js"></script>
<script src="jquery.interval.js"></script>
<script src="code.js"></script>
<script>jQuery.noConflict()</script>
<!-- load or insert other JavaScript here -->
This is assuming you actually need noConflict(), since your example shows no other libraries. I'm assuming you left them out of this question.
If you still get errors, this may be due to jquery.interval.js not being coded properly.
Upvotes: 0
Reputation: 284836
You're not defining a jcode
variable. $jcode
is entirely different. Choose one or the other. Sticking with $jcode
:
Also, as noted by cleek, use a separate (earlier) script
block for code.js:
<script type="text/javascript">
$jcode(document).ready(function() {
$jcode.interval(checkForms, 2000);
});
</script>
<script src="code.js" type="text/javascript"></script>
Upvotes: 1
Reputation: 874
The problem that you are encountering is in the use of the src attribute for the script tag that contains inline code. Either move the call to noConflict to code.js or create a new script tag that calls noConflict prior to referencing code.js.
<script>
jQuery.noConflict();
var $jcode = jQuery;
</script>
<script src="code.js"></script>
Also, as noted by others, be sure to use $jcode instead of jcode when referencing your jQuery alias.
John Resig discussed this in his Degrading Script Tags post.
Upvotes: 2
Reputation: 8463
<script src="jquery-1.4.2.js"></script>
<script src="jquery.interval.js"></script>
<script src="code.js">
$.noConflict();
</script>
jQuery(document).ready(function($) {
$.interval(checkForms, 2000);
});
Try this instead of $ symbol use jQuery
Upvotes: 0
Reputation: 13933
You are assigning jQuerys's $ to $jcode.
Therefore you should be using $jcode and not jcode (e.g $jcode(document).ready())
$jcode(document).ready(function() {
$jcode.interval(checkForms, 2000);
});
This should work.
Upvotes: 1