patricko
patricko

Reputation: 831

Appending a link with Jquery

I'm trying to add a link to my page depending on what page you're already on. I'm using Squarespace to build this site, so the easiest way for me to do this would be with Javascript or Jquery.

I think there is something wrong with this syntax that i'm missing. I already tried to break out of the quotes with a \, but that wasn't working. If I'm just outputting text, it works fine, but it seems to break when I try and make it work with a link.

 if(loc == pageOne) {
$("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
    }else{
  if(loc == pageOneB){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
  }else{
    if(loc == pageTwo){
     $("#pages").append("<div> <a href=\"http://design.optimus.com/projects\">Previous Page</a></div> ")
  }
 }
}

Edit: I just checked, and the links seem to be working in Chrome, but not Firefox.

Another Edit: So the link is working in Safari too. I guess the issue now is why not in Firefox? Is there something about this method that Firefox doesn't support?

Upvotes: 5

Views: 36099

Answers (3)

Bradley Mountford
Bradley Mountford

Reputation: 8273

Try this out:

switch(loc)
{
case pageOne:
  $("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>');
  break;
case pageOneB:
  $("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>');
  break;
case pageTwo:
  $("#pages").append('<div> <a href="http://design.optimus.com/projects">Previous Page</a></div>"');
  break;
}

Upvotes: 0

Jasper
Jasper

Reputation: 75993

Your code seems to work fine however you can use else if statements instead of nested if statements:

if(loc == pageOne) {
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
} else if (loc == pageOneB){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects?currentPage=2\">Next Page</a> </div>")
} else if (loc == pageTwo){
    $("#pages").append("<div> <a href=\"http://design.optimus.com/projects\">Previous Page</a></div> ")
}​

Here is a demo: http://jsfiddle.net/Rba6w/

Upvotes: 6

Deept Raghav
Deept Raghav

Reputation: 1460

see if this works

if(loc == pageOne) {
$("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>')
    }else{
  if(loc == pageOneB){
    $("#pages").append('<div> <a href="http://design.optimus.com/projects?currentPage=2">Next Page</a> </div>')
  }else{
    if(loc == pageTwo){
     $("#pages").append('<div> <a href="http://design.optimus.com/projects">Previous Page</a></div>')
  }
 }
}

Upvotes: 0

Related Questions