Jump to content

echo external javascript file


side1021

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

});

});

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.