side1021 Posted May 13, 2009 Share Posted May 13, 2009 Hi! Here is the problem that I found. In the php code I have a function that does this echo ' <script language="JavaScript" type="text/javascript" src="', $settings['theme_url'], '/js/superfish.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function() { $('ul.tincan').superfish(); }); </script> '; When I run this application, the javascript error(using firebug) I'm getting is "$('ul.tincan').superfish(); superfish() is not a function". I then took a closer look into the superfish.js and found out that it's a bit different than other js files that I've came across with. Its function starts out like this: $.fn.superfish = function(op){ ....... ....... ....... } The error only appears when "<script language="JavaScript" type="text/javascript" src="', $settings['theme_url'], '/js/superfish.js"></script>" is inside of an echo. And due to the design of the application, I have to leave it there. So then I tried again by copying the whole function out of that js file and into the <script> tag: echo ' <script language="javascript" type="text/javascript"> $(document).ready(function() { $.fn.superfish = function(op){ ....... ....... ....... } $('ul.tincan').superfish(); }); </script> '; This time no error or what so ever. So my questions are, What went wrong? Is it because echo can't handle external js file that has javascript function(s) stored in a variable? Is there away to code it so that I can use a external js file instead of putting all the javascript code in the <script> tag? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/ Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Wouldn't the single quote in $('ul.tincan') mess up the single quote you used for echo? Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833207 Share on other sites More sharing options...
side1021 Posted May 13, 2009 Author Share Posted May 13, 2009 sorry I did a quick copy and paste. that $('ul.tincan').superfish() did have character escape. $(\'ul.tincan\').superfish() PS. Superfish can be downloaded here: http://users.tpg.com.au/j_birch/plugins/superfish/ Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833241 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Is the file path to superfish.js correct? Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833244 Share on other sites More sharing options...
side1021 Posted May 13, 2009 Author Share Posted May 13, 2009 Yes, the path is correct. Firebug did show that superfish.js was loaded. I did try an explicit path name before going for the "copy all the code from superfish.js into the <script> tag" method. Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833250 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 My guess is the file path is not correct. Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833252 Share on other sites More sharing options...
side1021 Posted May 13, 2009 Author Share Posted May 13, 2009 Good news. I fixed it by using jQuery to do the loading of external javascript file. Here is the code:. $(document).ready(function() { $.ajax({ type: "GET", url: "http://domain.com/js/superfish.js", success: function(){$("ul.tincan").superfish();}, dataType: "script", cache: true }); }); Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833265 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 That is so lame. What's $settings['theme_url']? var_dump($settings['theme_url']); Also, I just noticed. This is not PHP-related. You posted this in the wrong forum. Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833267 Share on other sites More sharing options...
side1021 Posted May 13, 2009 Author Share Posted May 13, 2009 $settings['theme_url'] is a global variable in SimpleMachineForum source code. You are right...this is like 50% javascript and 50% jQuery. Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833313 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 jQuery is JavaScript. Can you please var_dump that and post it here? Quote Link to comment https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/#findComment-833321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.