简体   繁体   中英

Running a javascript function that is in an external .js file from within the script? (attached by src=) Using PHP

I'm using PHP code to generate the tag in the tags of my webpage.

I'm using PHP so that if a particular GET variable has been set (in this case, 'savedsearch') it runs a functions from the external javascript file I'm attaching to the web page.

Here's the code I'm using:

<?php
//check if a saved search has been used
if (isset($_GET["savedsearch"])) {
    $savedsearch=$_GET["savedsearch"];
    echo "<script type='text/javascript' src='search.js'>
    window.onload=sendsearch($savedsearch);
    </script>";
}
else {
    echo "<script type='text/javascript' src='search.js'></script>";
}
?>

This isn't working (doesn't run the function if the savedsearch is set)... is running a functions like this possible?

Is there any other way I can do this without having any javascript or PHP in the body of my page?

Help appreciated and thanks in advance.

If you have a script tag with a src attribute the code inside that tag is not executed, you'll have to add another tag to execute the code, a script tag can't be used to fetch a script and to run inline scripts.

You also are assigning the result of sendsearch() to window.load which is not a function. The only reason it's kind of working is because you don't really need to wait for onload event in your case? It's being called as the script is parsed, not after the window.onload event.

Also, be sure to call json_encode on the $savedsearch , that will prevent XSS and string escaping problems (while also adding the quotes around your string).

<?php
//check if a saved search has been used
echo "<script type='text/javascript' src='search.js'></script>";
if (isset($_GET["savedsearch"])) {
    $savedsearch=json_encode($_GET["savedsearch"]);
    echo "<script type='text/javascript'>
       // If you don't need to wait for the onload event
       sendsearch($savedsearch);
       // If you do need to wait for the onload event
       window.onload = function(){
         sendsearch($savedsearch);
       };
    </script>";
}
?>

Why dont you try:

<?php
//check if a saved search has been used
if (isset($_GET["savedsearch"])) {
    $savedsearch=$_GET["savedsearch"];
    echo "<script type='text/javascript' src='search.js'></script>
          <script>
            window.onload=sendsearch($savedsearch);
          </script>
         ";
}
else {
    echo "<script type='text/javascript' src='search.js'></script>";
}
?>

Having src and content both in the script tag at once is unreliable.

I'm not sure about the content/src problems outlined in the others' answers, but unless your sendsearch function returns a function this won't work. You'll need to have the following instead:

window.onload=function(){sendsearch($savedsearch)};

This provides a function which can be run when the onload event is called.

This might need to be used in conjunction with pulling the javascript into a different tag, as per the other answers - I'm not familiar with how that might work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM