Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays?

Status
Not open for further replies.

spperl

Programmer
Mar 29, 2005
34
GB
Hi,

I'm creating a web widget using a backend programming language to produce some javascript. The widget itself is a simple link rotator. The part I'm having a problem with is setting up my array. The output I generate is as follows:

Code:
var myquotes = new Array(
              "<a href="[URL unfurl="true"]http://linkOne">linkOne</a>",[/URL]
              "<a href="[URL unfurl="true"]http://linkTwo">linkTwo</a>"[/URL]
              );

Any ideas on how to escape the elements correctly?

Thanks...
 
Three ways:

1. use a backslash to escape the quotes within the assignment...

Code:
var myquotes = new Array(
              "<a href=[COLOR=red]\[/color]"[URL unfurl="true"]http://linkOne[/URL][COLOR=red]\[/color]">linkOne</a>",
              "<a href=[COLOR=red]\[/color]"[URL unfurl="true"]http://linkTwo[/URL][COLOR=red]\[/color]">linkTwo</a>"
              );

2. Use single quotes to assign your array elements

Code:
var myquotes = new Array(
              [COLOR=red]'[/color]<a href="[URL unfurl="true"]http://linkOne">linkOne</a>[/URL][COLOR=red]'[/color],
              [COLOR=red]'[/color]<a href="[URL unfurl="true"]http://linkTwo">linkTwo</a>[/URL][COLOR=red]'[/color]
              );

3. Use double quotes to assign your array elements and use single quotes in your href attribute:

Code:
var myquotes = new Array(
              "<a href=[COLOR=red]'[/color][URL unfurl="true"]http://linkOne[/URL][COLOR=red]'[/color]>linkOne</a>","<a href=[COLOR=red]'[/color][URL unfurl="true"]http://linkTwo[/URL][COLOR=red]'[/color]>linkTwo</a>"
              );


--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Hi

If that language happens to be PHP, I would use [tt]json_encode()[/tt]. But there are implementations in other languages too.
PHP:
[navy]$myquotes[/navy][teal]=[/teal][b]array[/b][teal]([/teal]
  [green][i]'<a href="[URL unfurl="true"]http://linkOne">linkOne</a>'[/URL][/i][/green][teal],[/teal]
  [green][i]'<a href="[URL unfurl="true"]http://linkTwo">linkTwo</a>'[/URL][/i][/green]
[teal]);[/teal]

[b]echo[/b] [green][i]"var myquotes = "[/i][/green][teal],[/teal] [COLOR=darkgoldenrod]json_encode[/color][teal]([/teal][navy]$myquotes[/navy][teal]),[/teal] [green][i]";\n"[/i][/green][teal];[/teal]


Feherke.
 
Personally I'd create two JS arrays, one for the text and one for the link itself and then you can use either PHP / JS to loop through these arrays and create the HTML rather than shipping / storing the HTML locally.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top