admin管理员组

文章数量:1279083

I don't know much about javascript (only done JAVA) but that havent keept me from doing a hack on some javascript code I found (from Google Bookmarks).

Here is my case: I'm creating a bookmark in my browser that contains a javascript instead of a URL link. When I press the bookmark I want it to open a new window with the same url but with some extra info. So say Im on and I press the bookmark, then it should open a window with the link: .DoYou/KnowYourABC - so ".DoYou" have been inserted just before the 3th "/"

I have the following script:

javascript:(function(){
    var a=window, b=document, c=encodeURIComponent,
    url = b.location,
    d = a.open(url, "bkmk_popup", "left="+
    ((a.screenX||a.screenLeft)+50)+",top="+
    ((a.screenY||a.screenTop)+50)+
    ",height=600px, width=1200px, resizable=1, alwaysRaised=1");
    a.setTimeout(function(){d.focus()},300)
})();

So far it opens a window with the same url. But I can't seem to split the url at the 3th "/". Have tried to get the index of the backslash in order to split the url and insert ".DoYou"

 i = url.indexOf("//",9)

but then the window will not open. Please help me out!

SOLUTION

javascript:( function(){ 
    var a=window;
    b=document;
    c=encodeURIComponent; 
    url = b.location; 
    var parts = window.location.href.split("/");
    parts[2] += ".DoYou";
    var newurl = parts.join("/");
    a.open(newurl, "bkmk_popup","left="+((a.screenX||a.screenLeft)+50)+",top="+((a.screenY||a.screenTop)+50)+",height=600px,width=1200px,resizable=1,alwaysRaised=1"); a.setTimeout(function(){d.focus()},300);
})();

I don't know much about javascript (only done JAVA) but that havent keept me from doing a hack on some javascript code I found (from Google Bookmarks).

Here is my case: I'm creating a bookmark in my browser that contains a javascript instead of a URL link. When I press the bookmark I want it to open a new window with the same url but with some extra info. So say Im on https://www.abcde./KnowYourABC and I press the bookmark, then it should open a window with the link: https://www.abcde..DoYou/KnowYourABC - so ".DoYou" have been inserted just before the 3th "/"

I have the following script:

javascript:(function(){
    var a=window, b=document, c=encodeURIComponent,
    url = b.location,
    d = a.open(url, "bkmk_popup", "left="+
    ((a.screenX||a.screenLeft)+50)+",top="+
    ((a.screenY||a.screenTop)+50)+
    ",height=600px, width=1200px, resizable=1, alwaysRaised=1");
    a.setTimeout(function(){d.focus()},300)
})();

So far it opens a window with the same url. But I can't seem to split the url at the 3th "/". Have tried to get the index of the backslash in order to split the url and insert ".DoYou"

 i = url.indexOf("//",9)

but then the window will not open. Please help me out!

SOLUTION

javascript:( function(){ 
    var a=window;
    b=document;
    c=encodeURIComponent; 
    url = b.location; 
    var parts = window.location.href.split("/");
    parts[2] += ".DoYou";
    var newurl = parts.join("/");
    a.open(newurl, "bkmk_popup","left="+((a.screenX||a.screenLeft)+50)+",top="+((a.screenY||a.screenTop)+50)+",height=600px,width=1200px,resizable=1,alwaysRaised=1"); a.setTimeout(function(){d.focus()},300);
})();
Share Improve this question edited Mar 5, 2012 at 14:37 Norfeldt asked Mar 5, 2012 at 14:09 NorfeldtNorfeldt 9,70824 gold badges109 silver badges172 bronze badges 3
  • 1 You might want to post un-minified javascript code. – Manu Clementz Commented Mar 5, 2012 at 14:13
  • 1 For reference, / is a regular slash. `\` is a backslash. – cHao Commented Mar 5, 2012 at 14:14
  • 1 / only needs to be escaped in regexpes, where is bees \/. In strings / is sufficient (it doesn't clash with anything). – pimvdb Commented Mar 5, 2012 at 14:16
Add a ment  | 

3 Answers 3

Reset to default 6

It can be done with a simple reg exp, using the parts of the window.location object [host/search/etc] or a simple split and join.

Here is the simple split and join solution.

var parts = window.location.href.split("/");
parts[2] += ".DOYOU";
var newStr = parts.join("/");
console.log(newStr);

You don't need to escape the / (forward slash).

i = url.indexOf("/",9)

P.S. The escape character is usually a backslash (\).

In a similar way to the accepted answer, if you don't really need to find the index, but want to place something near it, you can use string.replace(replacedtext, newtext) and RegEx.

For your URL example, if you wanted to add text before the first slash with word characters on either side, you could do

url = url.replace(/(\w(?=\/\w))/, '$1' + '.DoYou');

The $1 inserts the 1st parenthesized submatch string (see here for more explanation of the $).

Example

Like your issue, I wanted to do the same thing except in the same window, so I did:

javascript:void(location.href=location.href.replace(/(\w(?=\/\w))/,'$1'+'.DoYou'))

Having this as a bookmark allows me to just click the bookmark and take me to the page I want.

本文标签: Javascript Find index of slash () in a stringStack Overflow