Jump to content

[SOLVED] Will javascript not work on an ajax loaded page?


limitphp

Recommended Posts

If I use ajax to load a box or etc.  And that box is just a small php page, like playlist_ajx_songs.php

 

On that playlist_ajx_songs.php if I have

<script>

</script>

 

with a javascript function in their and I try to use that function, it doesn't work. 

Why is that?

 

It seems, you can only call functions that were already loaded before the ajax page was inserted.

 

Link to comment
Share on other sites

you need to do it a little differently:

 

<script type="text/javascript">
 foobar = function ( var1 ) {
   alert(var1);
 }
 foobar('test');
</script>

 

edit: this also assumes your AJAX code parses and eval()s JavaScript for you. if you are using a JS library for your AJAX (like jQuery) you should be all set

Link to comment
Share on other sites

you need to do it a little differently:

 

<script type="text/javascript">
  foobar = function ( var1 ) {
    alert(var1);
  }
  foobar('test');
</script>

 

edit: this also assumes your AJAX code parses and eval()s JavaScript for you. if you are using a JS library for your AJAX (like jQuery) you should be all set

 

I'm confused.

I have a page with a div on it

<div id='playlist_box'></div>

 

I use ajax to call a php page (ajax.php) to fill that div.

On the ajax page, if I put something as simple as :

<script>

  alert('test');

</script>

 

nothing will happen.  Its as if the ajax page ignores any javascript on it.

Link to comment
Share on other sites

What do you use to do the AJAX?

 

When a browser loads a page, it will look through the page for javascript and execute it. You loose this functionality with AJAX. Check out a JS Library like jQuery, and use it's AJAX functions. Their functions will parse the ajax response for JavaScript and eval() it so it gets run

Link to comment
Share on other sites

What do you use to do the AJAX?

 

When a browser loads a page, it will look through the page for javascript and execute it. You loose this functionality with AJAX. Check out a JS Library like jQuery, and use it's AJAX functions. Their functions will parse the ajax response for JavaScript and eval() it so it gets run

 

I'm pretty much using the example on phpfreak forums:

http://www.phpfreaks.com/forums/index.php/topic,115581.0.html

 

Why does the page that gets loaded via ajax lose its javascript functionality?

Link to comment
Share on other sites

Because the AJAX object doesn't care what is in the content.  It's only job is to load the content, not parse it.

 

 

You must tell the code to parse the other code (if that strange wording made sense).

 

 

Edit:  By the way, would this not have made more sense in the AJAX forum?

Link to comment
Share on other sites

Do it like this:

 

<html>
 <head>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
 </head>
 <body>
   <div id='playlist_box'></div>
   <input type="button" value="Load It" onclick="$('#playlist_box').load('ajax_page.php');" />
 </body>
</html>

 

jQuery will find and execute the JavaScript in ajax_page.php for you automatically

Link to comment
Share on other sites

Because the AJAX object doesn't care what is in the content.  It's only job is to load the content, not parse it.

 

 

You must tell the code to parse the other code (if that strange wording made sense).

 

What do you mean by parse the code?

 

I've tried looking at jQuery....it seems too complicated for me...

 

its was much easier for me to use the example from this forum.

Link to comment
Share on other sites

jQuery is easier than any non-framework AJAX code.

 

 

 

Anyway, you code PHP and you somehow don't know what parse means?  Huh?

 

 

 

Anyway, parsing, in regards to scripting languages like JavaScript (or PHP) is when the parser takes the code and turns it in to something useful.  It's essentially knowing what instructions to do based on certain 'tokens.'  For example, the PHP engine converts an echo construct into something useful.  Or, it may parse an fopen command into the system function call of fopen.

 

Trust me; using something predone to parse JS in AJAX will be much easier than trying to extract JS content from between <script> tags and eval() it.

 

 

 

A lot of times I see people use JS in AJAX returns when they don't really need to.  Are you sure you need to?

Link to comment
Share on other sites

jQuery is easier than any non-framework AJAX code.

 

 

 

Anyway, you code PHP and you somehow don't know what parse means?  Huh?

 

 

 

Anyway, parsing, in regards to scripting languages like JavaScript (or PHP) is when the parser takes the code and turns it in to something useful.  It's essentially knowing what instructions to do based on certain 'tokens.'  For example, the PHP engine converts an echo construct into something useful.  Or, it may parse an fopen command into the system function call of fopen.

 

Trust me; using something predone to parse JS in AJAX will be much easier than trying to extract JS content from between <script> tags and eval() it.

 

 

 

A lot of times I see people use JS in AJAX returns when they don't really need to.  Are you sure you need to?

 

I'm not sure yet...but there have been times when it seemed conveinet to just write the functions I needed on the ajax php page then on some global js page.

 

 

 

Link to comment
Share on other sites

if you don't use a framework, you have to use regex on the response body to pull out any bits that are between <script> tags, then run those fragments through JavaScript's eval() function.

 

did you even try the code i posted that uses jQuery? it works as is

Link to comment
Share on other sites

if you don't use a framework, you have to use regex on the response body to pull out any bits that are between <script> tags, then run those fragments through JavaScript's eval() function.

 

did you even try the code i posted that uses jQuery? it works as is

 

I'm sorry, I didn't realize you had the source for the jQuery sitting on a website ready to go.

 

Looking at that code, though, i need to explain something:

on my ajax page.....in divs or forms like on a submit button

<input type=submit >

putting in onclick in their works, or putting javascript in any of the objects in divs or whatever as onclick=, or onMouseover=

those all work.

 

Just the javascript in the <script></script> tags doesn't work.

 

Now that i understand why it doesn't work, you have to get it manuualy and eval it (thanks for all the info and explanation)....I'll just put all my functions I need to call in a global js file.

 

thanks for all the info.

Link to comment
Share on other sites

if that is what you want to do, go for it.

 

i'm still gonna push jQuery on you more though. i HIGHLY recommend trying it out when you get a chance. once you use it you will never go back

 

Ok.  I tried looking at it. 

So, I assume its a bunch of functions that ar in a js file...and anytime yuo want to use them you load that js file in your page?

 

And I assume the way they do things is alot easier than doing it by hand?

 

But I would still have to relearn everything.  I spent all this time learning javascript....now I would have to spend all this time relearning some functions that people wrote for javascript.

 

It seemed pretty darn complicated for me when i looked at it.

Link to comment
Share on other sites

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.