最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net ensure javascript is loaded once only - Stack Overflow

programmeradmin2浏览0评论

I've been working in asp webforms and I've been making a UserControl that depends on a small bit of javascript which is in an external file.

I want to be able to put a reference to the javascript inside the UserControl to ensure that it gets loaded on the page, but the problem is that the UserControl can appear multiple times on a page so I only want the script to be loaded with the first instance of the UserControl.

Is there an easy way to do this in ASP.NET Webforms?

I've been working in asp webforms and I've been making a UserControl that depends on a small bit of javascript which is in an external file.

I want to be able to put a reference to the javascript inside the UserControl to ensure that it gets loaded on the page, but the problem is that the UserControl can appear multiple times on a page so I only want the script to be loaded with the first instance of the UserControl.

Is there an easy way to do this in ASP.NET Webforms?

Share Improve this question asked Jan 6, 2010 at 23:26 lomaxxlomaxx 116k58 gold badges147 silver badges180 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Use the ClientScriptManager's RegisterClientScript() methods to handle this. In your case, since you're including the script from an external file, use RegisterClientScriptInclude().

Alternately, if you're using UpdatePanels, use ScriptManager's equivalent Register...() methods.

You could also handle this issue in the JS file itself, if you control its content.

var myObj = myObj || {};  // Variable to namespace your work

// Make sure we do our work only once
if (!(myObj.token)) {

    // do our work here...

    myObj.token = {};  // this ensure we won't enter the if block the next time

}
发布评论

评论列表(0)

  1. 暂无评论