简体   繁体   中英

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.

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);
});

尝试使用a元件?,与target="_blank"属性?

<a id="blue" href="www.website.com/projects/blue" target="_blank">blue</a>

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("#", "");

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

Try replacing # with 'projects/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM