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

Inserting javascript global variable in <div> tag in HTML body - Stack Overflow

programmeradmin1浏览0评论

I have the following tag in HTML:

<div data-dojo-type="dojox.data.XmlStore" 
     data-dojo-props="url:'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/6204/2', label:'text'" 
     data-dojo-id="bookStore3"></div>

I have the values 6204 and 2 in a couple of global variables in the script section:

<html>
  <head>
     <script>
      ...
      var newNeId = gup('neId');
      var newNeGroupId = gup('neGroupId');
      ...
     </script>
  </head>
</html>

Is it possible to have these variables in the div tag in the HTML body? If so, how?

To clarify this a bit more, I need to have the URL in the tag something like this:

url: 'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/'+newNeGroupId+'/'+newNeId

I have the following tag in HTML:

<div data-dojo-type="dojox.data.XmlStore" 
     data-dojo-props="url:'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/6204/2', label:'text'" 
     data-dojo-id="bookStore3"></div>

I have the values 6204 and 2 in a couple of global variables in the script section:

<html>
  <head>
     <script>
      ...
      var newNeId = gup('neId');
      var newNeGroupId = gup('neGroupId');
      ...
     </script>
  </head>
</html>

Is it possible to have these variables in the div tag in the HTML body? If so, how?

To clarify this a bit more, I need to have the URL in the tag something like this:

url: 'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/'+newNeGroupId+'/'+newNeId
Share Improve this question edited Jun 13, 2012 at 16:08 Shree asked Jun 13, 2012 at 15:39 ShreeShree 4,7477 gold badges40 silver badges49 bronze badges 1
  • @EricFortis These values e from the URL and I parse the URL and store these values in a variable. – Shree Commented Jun 13, 2012 at 15:42
Add a ment  | 

4 Answers 4

Reset to default 1

I changed it according to your requirement:

<html>
    <head>
        <script type="text/javascript">
            // example data
            var newNeId = 10;
            var newNeGroupId = 500;
            window.onload = function(e){
                var myDiv = document.getElementById("myDiv");
                myDiv.setAttribute("data-dojo-props", "url:'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/" + newNeId + "/" + newNeGroupId + "', label:'text'");
            }
        </script>
    </head>
    <body>
        <div id="myDiv" data-dojo-type="dojox.data.XmlStore" 
     data-dojo-props="url:'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/6204/2', label:'text'" 
     data-dojo-id="bookStore3"></div>
    </body>
</html>​

You could add them to the <div> using the same datalist pattern (MDN docu) as Dojo:

<div id="savebox" data-newNeId="6204" data-newNeGroupId="2"></div>

These attributes are then accessible by the element.dataset.itemName.

var div = document.querySelector( '#savebox' );
// access
console.log( div.dataset.newNeId );
console.log( div.dataset.newNeGroupId );

As @EricFortis pointed out, the question remains, why you want to do this. This only makes sense, if you pass those values on from the server side.

Take one parent div then set its id and then you can rewrite whole div tag with attributes using innerHTML.

document.getElementById('id of parent div').innerHTml="<div data-dojo-type=/"dojox.data.XmlStore/" 
 data-dojo-props=/"url:'http://135.250.70.162:8081/eqmWS/services/eq/Equipment/All/6204/2', label:'text'/" 
 data-dojo-id=/"bookStore3/"></div>";

you can append values you wants in innerhtml now.

here's simple native js code to do it

var body = document.getElementsByTagName('body')[0];
var myDiv = document.createElement('div');
myDiv.setAttribute('id', 'myDiv');
var text = 'newNeId: ' + newNeId + 
'<br/> newNeGroupId: ' + newNeGroupId';
body.appendChild(myDiv);
document.getElementById('myDiv').innerHTML = text;
发布评论

评论列表(0)

  1. 暂无评论