Jump to content

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/157968-echo-external-javascript-file/
Share on other sites

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

});

});

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.