开发者

JavaScript in widgets - how to handle?

I'm using ASP.NET to develop a webapplication. In this application I'm using a dashboard. And now I try to develop my own widget for this dashbord. But I don't understand how to do the javascript part.

In the widget I want to use the OpenLayers JavaScript map. It's created like this

<scri开发者_JS百科pt>
   var map = OpenLayers.Map("mapDiv");
</script>
<div id="mapDiv" />

Like this it works fine if I only want to use one widget at the same time. But if want to use it multiple times in the same dashboard, I get a lot of problem. I have two divs with the same id and two variables with the name map.

So the div id is no problem, I generate it dynamically with ASP.NET. But how do I handle the JavaScript part, so that I don't overwrite my "map" variable?

Hope someone understands my problem.

Thanks


If you have a way of defining generating a custom ID for the div, you should be able to use the same function to pass that ID to the script:

<% Dim myId As String = GenerateId() %>
<div id="<%= myId %>"></div>
<script type="text/javascript">
    this["<%= myId %>"] = OpenLayers.Map("<%= myId %>");
</script>

Essentially, the trick is that you can create dynamically-named variables by using the bracket syntax to define a new entry in the global context (the "this" variable).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜