1

My first post here, but I learned a lot already by lurking =) This time, I can't find a solution for problem, although it looks like something that's easy to achieve.

I have a list of links to blogpost, generated with Feed2JS. Unfortunately, the RSS feed that is the source for this adds anchors to the links that I don't want. I can't change the feed, because it's generated automatically in RapidWeaver.

Is it possible to remove everything from the hash in the url's with jQuery? for example: change

http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31

to

http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php

I'm pretty new to jQuery, and still have a lot to learn, so please keep your answer simple.

Jeroen

Jeroen
  • 11
  • 3

7 Answers7

1

if you just want to truncate the url, you can do it as

var url = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31";
var index = url.indexOf('#') != -1 ? url.indexOf('#') : url.length
alert(url.substring(0,index));

OUTPUT:

http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php

example : http://jsfiddle.net/2dXXx/

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
0

You don't need jQuery to do this as javascript supports it with window.location.

Have a look at this answer for what you are trying to do javascript window location href without hash? .

Community
  • 1
  • 1
startupsmith
  • 5,554
  • 10
  • 50
  • 71
0

I like doing it this way:

var a = document.createElement("a");
a.href = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31";
alert(a.protocol + "://" +  a.host + a.pathname + a.search);

http://jsfiddle.net/karim79/7pMbk/1

karim79
  • 339,989
  • 67
  • 413
  • 406
0

Sure you could do something like this:

var str = 'http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31';
var substr = str.split('#');
// substr[0] contains "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php"
// substr[1] contains "unique-entry-id-31"

Note that this is from the JavaScript API.

span
  • 5,405
  • 9
  • 57
  • 115
0

You don't need to use jQuery for that:

function truncate(href) {
  var a = document.createElement('a');
  a.setAttribute('href', href);

  a.hash = '';
  return a.href;
}
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
0

No dependency needed on jQuery, you can use a regexp, as demonstrated in this jsFiddle

var url = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.php#unique-entry-id-31"
console.log("base url >> " + url)    
var matcher = /(.*)#.*/.exec(url)
var truncated = matcher[1]
console.log("truncated url >> " + truncated)    
Grooveek
  • 10,046
  • 1
  • 27
  • 37
0
var url = "http://www.example.com/blog/files/398e042ea42b7ee9d1678b3c53132fc3-31.phpunique-entry-id-31";

alert(url.substring(0,url.indexOf('#'))); // will give you empty string if you dont have # in the url.

So, you can use

alert(url.split('#')[0]);
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
Virtual
  • 2,111
  • 5
  • 17
  • 27