[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69
  • Last Modified:

adding parameters to the url using jquery

Hi experts

I'm using jquery with a html page.

Right now I'm adding an image to my  page my using .prepend like this.

var myimage = $('<a href="http://www.google.com"><img class="testclass" src="Images/testimage.jpg" /></a>');
$('.TestDiv1').prepend(myimage);

This image is added correctly to my page and the hyperlink works.  It takes me to: http://www.google.com


What is the syntax to add some parameter to the url that are stored in a variable?

So i would have these variables

var parameter1 = "apple"
var parameter2 = "peach"

But I need the syntax to add these variables  to this url in the snippet below:

var myimage = $('<a href="http://www.google.com"><img class="testclass" src="Images/testimage.jpg" /></a>');
$('.TestDiv1').prepend(myimage);


so I want the hyperlink url to go to: http://www.google.com&apple&peach


Anyone know the syntax?
0
maqskywalker
Asked:
maqskywalker
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
so I want the hyperlink url to go to: http://www.google.com&apple&peach
This is an invalid URL. If you wish to add a query string to the URL, you must begin with a question mark and follow it with your key=value pairs, separated by ampersands. For example:
http://www.google.com?parameter1=apple&parameter2=peach

Open in new window

The simplest way to add the variables to the URL is to hard code them.
var myimage = $('<a href="http://www.google.com?parameter1=apple&parameter2=peach"><img class="testclass" src="Images/testimage.jpg" /></a>');

Open in new window

But I imagine the variables are variable, so you would need to concatenate the URL string.
var parameter1 = "apple";
var parameter2 = "peach";
var myimage = $('<a href="http://www.google.com?parameter1="+parameter1+"&parameter2="+parameter2"><img class="testclass" src="Images/testimage.jpg" /></a>');
$('.TestDiv1').prepend(myimage);

Open in new window

0
 
maqskywalkerAuthor Commented:
thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now