简体   繁体   English

向.js文档中添加onclick和其他代码

[英]Adding onclick and additional code to a .js document

I have a pretty specific question. 我有一个非常具体的问题。 I am trying to implement an onclick and cross domain tracking within a block of text, but it looks like it may need to be put directly into a .js document. 我正在尝试在文本块内实现onclick和跨域跟踪,但似乎可能需要将其直接放入.js文档中。 I don't have a lot of JS experience. 我没有很多JS经验。 Basically, the current code looks like: 基本上,当前代码如下所示:

// JavaScript Document
function popup_no_status(loc)
{
var windowW=1000
var windowH=700
s = "width="+windowW+",height="+windowH+",status=yes, resizable=yes, scrollbars=yes";
mywin = window.open(loc ,'CBE', s);
mywin.focus();
}

What I want to add to this is: 我要添加的是:

onclick="pageTracker._trackEvent('Button', 'Click', 'QuickSearchWidget'); pageTracker._link(this.href); return false;

Can I just add it to the end of the document before the closing bracket? 我可以将其添加到文档末尾的括号中吗? Any Ideas? 有任何想法吗?

Much appreciated! 非常感激!

As long as the object pageTracker is defined and instantiated, you can call its methods like any other function: 只要定义并实例化了pageTracker对象,就可以像调用其他任何函数一样调用其方法:

function popup_no_status(loc) {
    var s = "width=700,height=1000,status=yes, resizable=yes, scrollbars=yes";
    var mywin = window.open(loc ,'CBE', s);
    mywin.focus();

    pageTracker._trackEvent('Button', 'Click', 'QuickSearchWidget');
    pageTracker._link(this.href);
}

Also, the variables windowW and windowH are pointless in your example code - there is no need to store the string values in a variable if all you're going to do is concatenate them into another string. 同样,变量windowWwindowH在您的示例代码中没有意义-如果您要做的只是将它们连接到另一个字符串中,则无需将字符串值存储在变量中。 Further, unless you intend the mywin and s variables to be global, you should use the var keyword before defining them - that restricts the variables to the function scope instead of the global scope (all variables declared in a function without the var keyword are considered global). 此外,除非您打算将mywins变量设置为全局变量,否则应在定义它们之前使用var关键字-这样mywin变量限制为函数范围而不是全局范围(考虑在函数中声明的所有不带var关键字的变量全球)。

If the code above gives an error like ReferenceError: pageTracker is not defined , that means that the code in which the pageTracker object is defined is either not included on the page, or it has not been instantiated. 如果上面的代码给出类似ReferenceError: pageTracker is not defined的错误,则表示未定义pageTracker对象的代码在页面上,或者尚未实例化。

Now... as for onClick, I am not clear what you're after here. 现在...至于onClick,我不清楚您在这里追求什么。 Do you want this function to run when someone clicks the document? 您是否要在有人单击文档时运行此功能? That would get pretty annoying! 那会很烦人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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