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,048
  • 7
  • 40
  • 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,184
  • 10
  • 47
  • 70
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
  • 334,458
  • 66
  • 409
  • 405
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,509
  • 7
  • 52
  • 110
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
  • 29,989
  • 8
  • 97
  • 120
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
  • 9,903
  • 1
  • 26
  • 36
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,048
  • 7
  • 40
  • 58
Virtual
  • 2,033
  • 5
  • 17
  • 27