I have a plugin which shows posts from a certain city:
<div id='banner-root' style="height: 50px; width: 385px;"></div>
<script src="http://lujanventas.com/plugins/banner.js" type="text/javascript"></script>
I need to pass the cityID to the external javascript. I'm already getting height and width on the external .js by simply checking the banner-root element.
How do I pass the and ID? Also it would be nice if it was clear that you are passing the cityID
-
1How are you passing the width and height?j08691– j086912012年05月30日 18:52:38 +00:00Commented May 30, 2012 at 18:52
-
I get it by checking the div's height and width. What happens if I add an attribute to the script like 'city'. Can I get that with js?lisovaccaro– lisovaccaro2012年05月30日 18:55:25 +00:00Commented May 30, 2012 at 18:55
-
1I don't know. I've read your question three times and I can't make heads or tails of it.j08691– j086912012年05月30日 18:55:59 +00:00Commented May 30, 2012 at 18:55
2 Answers 2
You don't really pass the width and height to the script according to your comments.. you can use the same style with cityId like so:
<div id='banner-root' style="height: 50px; width: 385px;" data-cityId="32"></div>
You can then retrieve it with
document.getElementById("banner-root").getAttribute("data-cityId");
Comments
When I had to do so, I used this:
<script type="text/javascript">var cityID = x;</script>
<script src="http://lujanventas.com/plugins/banner.js" type="text/javascript"></script>
Then I can access cityID in the banner.js script
EDIT
One of Javascript flaws are - everything by default is in global scope.
This means in the example above cityID is devined in global namespace - window. This is great for example when I want to reach this value from banner.js but bad by design as missed var statement in function body can overwrite global variable.