?
Solved

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

Posted on 2011-09-20
8
Medium Priority
?
256 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 1500 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

764 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