开发者

Uncaught ReferenceError: parseURL is not defined

I've defined a function named parseURL in javascript in the head of my file, and I have a button set so that when it is clicked, it should call said function. However, for some reason my debugger is telling me that parseURL is not defined. The exact error is "Uncaught ReferenceError: parseURL is not defined"

<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
    <link rel="stylesheet" href="../css/style.css" type="text/css" />   
    <TITLE>
    Pic Grabber (javascript)
    </TITLE>
<script type="text/javascript" src="linkedURL.js" />

<script type="text/javascript">
var Page = {};
Page.currentURL = null;
Page.link = document.getElementById("link");

/*
###################################
# The function that "doesn't exist"
###################################
*/

function parseURL(){
    Page.url = document.getElementById("url").value;

    if(!checkValidURL(Page.url)){
        alert("Must Enter a valid URL");
    }

    Page.link.innerHTML = Page.url;

    return true;
}

function checkValidURL(url){
    var request = false;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest;
    } 
    else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHttp");
    }
    else{
      开发者_JS百科  alert("FAIL");
    }

    if (request) {
        request.open("GET", url);
        if (request.status == 200) { 
            return true; 
        }
    }

    return false;
}

</script>
</HEAD>
<BODY>
    <div class="wrap">
        <h1><a href="#">My Database</a></h1>
        <ul id="menu">
            <li><a href="../index.php">Home</a></li>
            <li><a href="../upload.php">Upload</a></li>
            <li><a href="#">Advanced Search</a></li>
            <li><a href="#">My Profile</a></li>
        </ul>
        <div class="clear"></div>
        <div id="left">
            <a href="#" id="link"></a>
        </div>
        <div id="right">
            <div class="box">
                <form id="input">
                <p>URL:</p><br/>
                <input type="text" id="url" /><br />

                            #################################
                            # This is where the error appears
                            #################################

                <input type="button" id="getButton" value="Get Pics" onclick="parseURL()" />
                <input type="button" id="back" value="<< Back" onclick="back()" disabled="true"/>
                <input type="button" id="next" value="Next >>" onclick="next()" disabled="true"/>
                </form>
            </div>
        </div>
    </div>

</BODY>

</HTML>

It seems like it should be an easy fix, I've tried a bunch of things though and nothing seems to be working. Any help is appreciated.


Move this:

<script type="text/javascript" src="linkedURL.js" />

to here:

<script type="text/javascript" src="linkedURL.js"> </script>
</BODY>


Do you actually have those hashes in your JavaScript? If so, they'll kill your script completely.

If you want to comment in JavaScript, you can use single line comments:

// This is a comment.

or multi-line comments:

/* This is
a comment */


I can see a few problems in that js, one of which may be causing the error :

  1. You should not write , but always use . Some browsers are quite stupid.
  2. You call document.getElementById("link") right at the beginning of the script. When that line executes, there is no element in the document having id link ... there is no element in the document at all. A naive solution is to move all the scripts at the end of the page, while the formally correct solution is to use the onload event.
  3. Javascript does not support comments with #. It supports // for single lines, and /* .. */ for multiple lines.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜