开发者

Included php file calling Javascript function

开发者 https://www.devze.com 2022-12-29 18:08 出处:网络
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

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消