简体   繁体   中英

Included php file calling Javascript function

Here's the deal. I've got index.php which links to an internal JS file in it's header. index.php then includes another .php file, which outputs this: + add file. addFile() is a Javascript function defined in the external JS file.

By doing this nothing happens, the included php does not "see" the JS function. Encapsulating the JS in the included PHP makes it all work. But I don't want to do it that way.

Any ideas?

EDIT:

here's the source

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Archie</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" href="/screen.css" type="text/css" media="screen"/>
        <script src="/lib/js/archie.js" type="text/javascript"></script>
    </head>
    <body>
        ...
        ...
        //included php starts here
        <form action="/lib/course.php" method="post">
            <fieldset>
                    <div id="addFileLocation"></div>
                <a href="#" onClick="addFile()">+ add file</a>          
                <input type="hidden" id="addFileCount" value="0"/>
            </fieldset>
        </form>
        //ends here
        ...
        ...
    </body>
    </html>

and the js:

<script type="text/javascript">
    //Dynamically add form fields

    //add file browser
    function addFile() {
        var location = document.getElementById('addFileLocation');
        var num = document.getElementById('addFileCount');
        var newnum = (document.getElementById('addFileCount').value -1)+ 2;
        num.value = newnum;
        var newname = 'addFile_'+newnum;
        var newelement = document.createElement('input');
        newelement.setAttribute('name',newname);
        newelement.setAttribute('type','file');
        location.appendChild(newelement);
    }

</script>

It's possible that the file has not been "loaded" by the browser at the point the function is called. That's why I use jQuery's $(document).ready() function. If you wrap your call to addFile in a function similar to jQuery's "document ready" (which simply waits until the whole document has been downloaded by the browser to execute js code), then all of your functions should be there. What happens when you wait until the whole thing is loaded and then manually call that function in the browser's address bar?

After looking at the code sample, maybe your href="#" is short circuiting the onClick handler. What happens if you change the anchor to:

            <a href="javascript:addFile();">+ add file</a>  

Got it. Silly me, I forgot to remove the <script></script> pair from the external js file. Thanks for your help Rich!

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