0

I have the problem when i echo this:

echo "Logged in! <script> alert('hello'); </script>";

The message "Logged in!" appears, but not the alert. How can i fix it so i get the alert? I can't use header(); because i already echod things out!

I also tried multiple thing like:

echo "Test message <script> window.location.href = 'index.php';"

Same thing again, Test message was echo'd, but the script wasn't run.

I hope someone can help me!

Edit:

NOTE: All of this code is in a xml file that i get that response of and put that in a div. So the script is in a message that i get with responseXML and output the data in a div.

Question i have in 1 sentence: How can you run a javascript function in a ajax call without jquery?

asked Nov 5, 2014 at 13:48
13
  • Add ob_start(); at begining of your file and then you can use header(..); Commented Nov 5, 2014 at 13:52
  • Are you outputting only this? Did you try adding the "basics" html tags around it? (w3schools.com/tags/tag_html.asp) Commented Nov 5, 2014 at 13:57
  • I am outputting this in a big html file with alot of html. I output this in a specific div. Commented Nov 5, 2014 at 14:00
  • ob_start(); Is not working for me...? code: ob_start(): echo "Logged in!"; header("Location: http://google.com"); Commented Nov 5, 2014 at 14:06
  • Is there any other javascript in the page? Maybe one of them could be causing an error that makes the browser stop executing all other javascript on the page. Commented Nov 5, 2014 at 14:08

3 Answers 3

1

I'm guessing you call this script through ajax after the page is already loaded. In this case it's not surprising the script isn't running because the browser runs the scripts as it reads them and isn't on standby for another script tag to appear.

If this is the case, you can solve this by adding some event listener or even better, call a desired function in the end of the ajax response.

answered Nov 5, 2014 at 14:15
Sign up to request clarification or add additional context in comments.

3 Comments

How does a desired function work without jquery but with normal javascript? Sorry i am pretty new to ajax
@LoganMurphy You're right, and your example works perfectly. However it's important to mention that while adding a DOM script element works, adding the script as plain text will not yield the same results.
The append method creates a DOM element, same as pure js
0

Your PHP looks fine, but you might need to change the way that you collect the JavaScript.

Consider this example: http://jsfiddle.net/0vcewvkc/1/

If you are collecting JavaScript only, you can wrap the response in a jQuery tag ($()), and then append it to your document.

$("#go").click(function() { // call the function here
 // your ajax code would go here, but either way you will end up with a string of JavaScript
 var response = '<script>alert("hi");</' + 'script>';
 // use jQuery to append this script to your document's body, like so:
 $(response).appendTo(document.body) 
});

Update

To do this without jQuery, the end of your AJAX call would have this code:

 var response = 'alert("hi");'; // note that we have removed the <script> tag
 var s = document.createElement("script"); 
 s.innerHTML = response;
 document.getElementsByTagName('body')[0].appendChild(s);
answered Nov 5, 2014 at 14:25

4 Comments

Do you need to do this with jquery? Because i already done everything with normal javascript....
@Nytrix I just updated my answer with a non-jQuery option. You can see it in action here: jsfiddle.net/0vcewvkc/2 . I did the button click with jQuery, but the inner code should work fine without it.
I am probaly not getting this right but, when i do it like this: echo "Logged in!"; echo " <script> var response = alert(''); var s = document.createElement('script'); s.innerHTML = response; document.getElementById('outputLogin').appendChild(s); </script> ";
@Nytrix : "AJAX call" refers to the code that gets the responseXML, not the code within the responseXML itself. My code should be added to the calling page (Page 1, from my comment above). Page 2 should only echo the JavaScript that you want to execute (not any test text, like "Logged in!"). It might be helpful to modify your question to include the code from Page 1.
0

try the following this should work

<?php 
echo 'Logged in ';
?>
<script> alert("hello")</script>;
<?php
 // the rest of your php code
?>
answered Nov 5, 2014 at 14:09

1 Comment

@Nytrix then im sorry i cant help you my knowledge is limited to php mysql js and css but you could try a google search

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.