简体   繁体   English

asp.net主页中的Javascript代码取决于当前打开的页面

[英]Javascript code in asp.net master page dependent on currenty open page

I have a problem with my JS script. 我的JS脚本有问题。 I have written a function in my Site.Master because I use it in every page of my website. 我已经在Site.Master中编写了一个函数,因为我在网站的每个页面中都使用了该函数。 Right now I need to add a new line to that function (execute another function) but, and this is the whole problem, the function to be executed is only defined in one of my content pages. 现在,我需要向该函数添加一行(执行另一个函数),但这就是整个问题,要执行的函数仅在我的一个内容页面中定义。

To be honest it doesn't even cause any problems for me, everything works correctly, but I don't want to leave a call to the non-existing function in the rest of my content pages. 老实说,它甚至不会给我造成任何问题,一切正常,但是我不想在我的其余内容页面中调用不存在的函数。 So what I need is to define somehow that the last line of my master page function well be only executed if the page I am on is the one with the definition of that function. 因此,我需要以某种方式定义我的母版页函数的最后一行仅在我所在的页面是具有该函数定义的页面的情况下才能执行。

Any ideas how to do that? 任何想法如何做到这一点?

I am sorry for so confusing explanation of the problem... 对这个问题如此混乱的解释,我感到抱歉。

You could try the following: 您可以尝试以下方法:

if (typeof MyFunction == 'function')
{
    MyFunction();
}

This will only call MyFunction if it exists on the current page. 如果当前页面上存在MyFunction,它将仅调用MyFunction。

Just check to see if the function is there before you call it. 在调用它之前,只需检查一下该函数是否存在。 if (typeof foo != 'undefined') foo()

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

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