Can't change url in href with jquery - javascript

Basically, I'm trying to replace a part of url in the middle ('#') with another one, and remove first part of url. Script replaces '#' with no problem but rfuses to remove the first part of url. Am i doing something wrong, or is there another solution for that?
html of it
<a class="link_imagelibrary" href="#pink_yellow_flowers.pdf?5612">Download pdf set</a>
on condition changes to
<a class="imgdownload" href="http://www.picturewall.com/pages/Botanicalhttp://#.com/s/files/1/0183/2687/files/passpink_yellow_flowers.pdf?5612">Download pdf set</a>
jquery
$(".imgdownload").each(function(){
this.href = this.href.replace('#', 'http://#.com/s/files/1/0183/2687/files/pass');
this.href = this.href.replace('http://www.#.com/pages/botanical', '');
});
It's supposed to happen on condition. Judging that the rest of the script works fine on the condition- looks like the problem is somewhere here

This line is not being matched
this.href = this.href.replace('http://www.picturewall.com/pages/botanical', '');
because it doesn't exist in your href: (Note the capitalisation of 'Botanical')
"http://www.picturewall.com/pages/Botanicalhttp://cdn.shopify.com/s/files/1/0183/2687/files/passpink_yellow_flowers.pdf?5612"

The href is expanding with this.href, so while you have:
<a href="#" ...
The DOM element's href property gets this:
<a href="http://example.com/your/page/wherever.php#" ...
The easiest way to handle this is to do it differently:
$(".imgdownload").each(function(){
this.href = 'http://cdn.shopify.com/s/files/1/0183/2687/files/pass' +
this.href.split('#').pop();
});
Here is a demonstration of the "problem":
$('a').each(function ea(){
console.log(this.href, this.getAttribute('href'));
});
http://jsfiddle.net/Gtr8V/
You'll note that element.getAttribute('href') does return the raw href attribute value, not the property. You could use this with .replace() instead of the this.href.replace() you were trying.

Related

Change Custom word from anchor tag href attribute

I Transfer my website from www.guruji.com/demo to guruji.com
now my all links is home about
i want to remove demo from all link
how can i do this Please Solve
$('a').each(function() {
var url = $(this).attr('href');
url = url.replace("demo/", '');
$(this).attr('href', url);
})
This basically says loop over every a tag, take the href value, remove the demo/ part, and replace it with url.
The ctrl+H method suggested by Flo is easier imo

Javascript - How to replace a certain occurence of a anchor's href contents?

I'm using a widget (Purechat) that allows customers and operators to communicate to each other. I've ran into an issue where anchors' href values inside this widget are being appended with "http://%20", thus making them unclickable to our users. We are investigating the code, however, I would like a quick fix for this by replacing all href contents that contain "http://%20" and replace that portion of the href with an empty string so my anchors work.
What would be the best way to go about this?
$('a').attr('href', function(index, value) {
return value.replace("//%20", "");
});
You can run a foreach jquery function which runs over every anchor whose href starts with that string, then cut it with substring method and set it's href value again.
This should work:
$("a[href^='http://%20']").each(function(){
var oldHref = $(this).attr('href');
var newHref = oldHref.substring(10, oldHref.length);
$(this).attr('href',newHref);
});

jQuery: adding prefix to any url (.attr)

I cant seem to figure this out as simple as I am sure it is, but I am trying to use jQuery to modify all URL's for selected elements, not to replace, but to add something before the link. I was curious when i stumbled on
blankrefer.com.
I am trying to make links more secure by changing them like so:
from:http://www.blah.com/blah
to:http://blankrefer.com/?http://www.blah.com/blah
I have this code that adds after particular element URL's but I am not sure about adding for a pretence:
$("A.blah").attr("href", function(i, href) {
return href + '/blah';
});
try this one, will change href for all anchors having blah class
$("A.blah").each(function(){
$old_url = $(this).attr('href');
$new_url = 'http://blankrefer.com/?'+$old_url;
$(this).attr('href',$new_url);//changed this line
});

Replace HTML attribute value with regular expression in JavaScript

I want to replace the HTML attribute double quote with single quote. For example:
If the input is:
<a onclick="Track("http://www.example.com")">Track Me</a>
The output will be:
<a onclick='Track("http://www.example.com")'>Track Me</a>
Here the onclick="Track("http://www.example.com")" needs to be replaced with onclick='Track("http://www.example.com")' i.e the only change here is onclick="..." is replaced with onclick='...'.
In my case the content is retrieved from an HTML editor whose content needs to be cleaned in client side using JavaScript. The only way I can currently think of is I need to use Regex to replace the onclick double quote attribute with single quote attribute and it should only happen when "Track" function is used in onclick attribute.
I have tried something like: jsFiddle
I am looking for any solution. Any help is highly appreciated.
Thanks.
If you cann't change others,then change yourself.
so here we go:
var str = '<a onclick="Track("http://www.example.com")">Track Me</a>';
console.log(str.replace(/onclick="Track\("([^"]+)"\)"/, 'onclick=\'Track("$1")\''));
If You are still looking for regular expression, You can use this example. It will give You that link <a onclick="Track('http://www.example.com')">Track Me</a>
var s = '<a href="#" target="_blank"
onclick="Track("http://www.example.com/")">Test Link</a>';
var p = s.replace(/(onclick=")(.*)(\")/,function(match,prefix,handler,suffix){
return prefix + handler.replace(/"/g,'\'') + suffix;
});
console.log(p);
And fiddle for demo;
I did not see, that You look for version <a onclick='Track("http://www.example.com")'>Track Me</a>, so you can change above code with this line
var p = s.replace(/(onclick=")(.*)(\")/,function(match,prefix,handler,suffix){
return prefix.replace(/"/,'\'') + handler + suffix.replace(/"/,'\'');
});

Proper Syntax to Pass Vars in a href JavaScript

Basic question so I feel dumb but..., Whats the proper syntax below in the href?
The href:
<html>
</html>
The Function:
function navClickListener(appendE, target, gotoURL) {
//doing stuff
};
When you really have to use inline JavaScript, use different quotes, eg ' or ".
Currently, the HTML attributes are marked by double quotes, as well as the JavaScript code.
Is effectively truncated to:
^ Because of this.
In this case, since you're using a JavaScript-URI, you can also use %22 instead of double quotes.
Demo: http://jsfiddle.net/pu3CM/
You'd be better off avoiding JavaScript in your "href" attributes.
<a href='#' onclick='navClickListener("navContent", "bodyContent", "http://192.168.1.34/wiki/index.php/Airworthiness_Directive #content"); return false;'>Click Me</a>
Using javascript:void(0); as the HREF value will prevent jumping or other undesired behavior from happening when the user clicks on the anchor. Use single quotes since you have double quotes in your JavaScript.
<a href="javascript:void(0);" onclick='javascript:navClickListener("navContent", "bodyContent" "http://192.168.1.34/wiki/index.php/Airworthiness_Directive #content");'></a>
Alternatively, you can do the entire thing in your JavaScript by binding a click handler. This would allow you to set a normal HREF value, which would be better for screen readers, and still allow the same functionality.
$(document).ready( function() {
$('.someclass').click( function(event) {
event.preventDefault();//Does the same thing as javascript:void(0); in the HREF value
var pageURL = $(this).attr('href');
navClickListener("navContent", "bodyContent", pageURL );
} );
} );

Categories

Resources