Solved

What is the syntax of including a variable in document.write statement?

Posted on 2011-09-20
8
254 Views
Last Modified: 2012-05-12
I have several localStorage variables set. There names and values are as follows:

localStorage.trxCtr = 2
localStorage.item1 = 1000
localStorage.item2 = 2000

Below is the code that I am currently trying to use. I want to loop for the value of my counter and create an <li> with the corresponding localStorage item.

<ul>
   <script type="text/ecmascript">
   var i =1;
   for (i=1;i<=parseInt(localStorage.trxCtr);i++);
   {
       document.write("<li><a href=getitemdetail.php?item="localStorage["item" + i ]">something here</a></li>");
   }  
  </script>
</ul>

I need the syntax to set the value of item in my <a href> statement.
0
Comment
Question by:kbios
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36570822
Test page : http://jsfiddle.net/88eJ2/1/
<ul>
    <script language="javascript" type="text/javascript">
    localStorage = {};

localStorage.trxCtr = 2
localStorage.item1 = 1000
localStorage.item2 = 2000
    var i = 0;
    for (i=1;i<=localStorage.trxCtr;i++)
    { 
        document.write("<li><a href='getitemdetail.php?item=" + localStorage["item" +i] + "'>" + localStorage["item" +i] + "</a></li>");
    }

</script>
</ul>

Open in new window

0
 

Author Comment

by:kbios
ID: 36570849
In my code I do not have it as a javascript. According to a previous GURU there is no javascript needed so I'm using the regular script (ecmascript). I would prefer to use this due to some previous display issues I was having when using javascript.

The localStorage values are set in the client side localStorage area they do not need to be set in the javascript you supplied. Please look at this as not a javascript but as a ecmascript. How can I use the localStorage values within the <a href> tag?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36570859
emacscript tag : http://jsfiddle.net/88eJ2/2/

http://en.wikipedia.org/wiki/ECMAScript

>The localStorage values are set in the client side localStorage area

no difference, did you try it?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:kbios
ID: 36571012
This HTML program IS running on the client side. I'm capturing data to the localStorage variables through a process on the client side. Once I have the localStorage variables I want to loop through the localStorage and retreive the data values and present them in an unnumbered list <ul><li><a href........> for each item. Then I'll select the item from the list which will call a php program and pass the item number to it.
0
 

Author Comment

by:kbios
ID: 36571020
yes I ran the code. the <li> shows up undefined. it's not retrieving the value from localStorage.
0
 

Author Comment

by:kbios
ID: 36571325
I was FINALLY able to get this code to work:

<ul>
  <script type="text/javascript">
  var i=1;
  do
  {
   document.write("<li><a href='getitemdetail.php?item=" + localStorage["item" + i] +"'>" + localStorage["item" + i] +"</a></li>");
   document.write("<br />");
   i++;
  }
 while (i<=(parseInt(localStorage.trxCtr)));                        
 </script>
</ul>

I have 1 final question before I accept the solution. I tried the above code using the for loop and the while loop and NEITHER one worked. I tried the do...while loop (above code) and it works great!!! Any insight as to why the other loops didn't work. Both the for and the while would not display each of the localStorage records ONLY the last record. Any insight would be GREATLY appreciated.

Thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36572191
The other loop use i<=
Must be : i<
0
 

Author Closing Comment

by:kbios
ID: 36572953
Actually both loops are set to i<= neither one of them are or were i<. Even if it were i< there still should have been multiple <li> being displayed. In this case the bottom line was that neither the for or while loops worked. This code only worked with the do...while.

I appreciate your effort in trying to help me solve this problem. Thanks.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question