开发者

javascript calling object

I'm kind of new to this so a bit confused.

I have a js file named rrr.js, in which I have this code:

var rrr_rrr2=
{
    // get the domain name from the current url
    get_d开发者_高级运维omain_name:function()
    {
        //code here...
    },
// other functions here
}

Now in my HTML page I simply added it like I usually do:

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

and called it like this:

Step 1 completed!<br><br>Click <a href="javascript:rrr_rrr2.get_domain_name()">here</a>

But that does not work... what am I doing wrong?

(by the way, this is in a firefox addon. FF gives me this error:

Error: rrr_rrr2 is not defined
Source File: javascript:rrr_rrr2.get_domain_name()
Line: 1

Help please!


I think Satyajit was almost right. Try closing it like this instead:

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

But also, if this js file is part of your addon, you can't access it directly from an HTML page, unless you put it at a resource: URI or something. Read up on privileged vs. unprivileged code.


Could it be that you have not closed the tag like so

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


Nothin is wrong with the code you have just shown, I have reproduced it in this JsFiddle.

There is one thing to look out for: the variable rrr_rrr2 must be in the global scope to access it with the javascript: href. If it is defined inside of a function, for example, then it is not in the global scope and cannot be accessed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜