如果从特定的jsp调用,请检查javascript

时间:2012-11-22 04:36:32

标签: javascript

我有一个javascript文件global.js,这是一个常见的js文件。

我在名为test this javascript file的函数中添加了几行代码。

问题是我希望只有在从名为home.jsp的特定jsp而不是任何其他jsp进行调用时才执行这些代码行。

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:0)

您可以将网页名称检查为

var PagePath = window.location.pathname;
var PageName = PagePath.substring(PagePath.lastIndexOf('/') + 1);
if(PageName == "YourDesiredPage")
{
        // Execute particular lines of code here
        // Do Whatever you want
}

答案 1 :(得分:0)

一个明显的解决方案是从该文件中获取这些行,并单独包含它。这是推荐的。

另一种可能性是检查location并有条件地执行。

另一个,在脚本标记之前定义一些变量;或使用this technique在脚本网址中传递它。