opening a new window based on the current url of the page

So ostensibly I’m trying to make a button or a link who’s target is contingent on the current page’s URL. I’m working on a WordPress portfolio site that opens up different projects in an Ajax window, but I also want to be able to link to the separate project page from that window. For instance, if I click the thumbnail for a project titled “Blue” it opens up the project in the ajax window and the url changes to “www.website.com/#blue.” Incidentally, the url of the corresponding project page would then be “www.website.com/projects/blue”. The idea is to hardcode the button into the Ajax window and write a script that generates the correct URL for the project page so my client doesn’t have to copy-paste the code for the button and update the target URL every time she posts a project. This is what I came up with, but I’m not great with Jquery or Javascript and I think something might be wrong with my syntax or the structure of the script. Right now, nothing happens when I press the button.

First it splits the url at each “/” and creates an array from the different strings, then it removes the “#” from the unique string, and opens a new window with the new address.

Read More

EDIT There were some syntax errors, but it’s still not working. Any thoughts on this new version:

$(".comment_button").click(function(){
var parse_url = window.location.href.split('/');
var project_name = parse_url[2].replace("#", "");
window.open("http://www.balletinform.com/projects/" + project_name);
});

Related posts

3 comments

  1. Tried using a element ?, with target="_blank" attribute ?

    <a id="blue" href="www.website.com/projects/blue" target="_blank">blue</a>
    
  2. If I understand correctly, what you want to get is the #blue part of the url.

    If so, you can use window.location.hash.

    Your function will then looks like window.open("http://www.balletinform.com/projects/" + window.location.hash.substring(1));

    Your current function was setting project_name to "www.balletinform.com" ([0]=>"http:"; splitted(/); [1]=>""; splitted('/'); [2]=>"www.balletinform.com"; splitted('/'); [3]=>"#blue").
    So an alternative solution would have been var project_name = parse_url[parse_url.length-1].replace("#", "");

  3. var new_url=""+(window.location.href).replace('#','projects/');
    window.open(new_url);
    

    Try replacing # with ‘projects/’

Comments are closed.