URL being encoded wrong - javascript

I'm getting confused passing the http:// in a string to the url as it is being stripped to
http%3A%2F%2F
I tried.. using the encodeURIComponent(http://)
but that didn't work either..
I'm trying to pass the url into here:
https://www.facebook.com/sharer/sharer.php?url=
Here is my code that isn't working:
$(document).on('click', '.fb', function(e) {
var findfb = $('.fb').parent().parent().parent().find(".zoomy").attr("src");
var facebook_url = 'http://www.facebook.com/sharer.php?url=http://www.site.com/folder1/'+encodeURIComponent(findfb)+
'&title='+encodeURIComponent('title of page');
window.open(facebook_url);
});

Just do it as simple as:
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb +'&title=' + 'title of page');

var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb) + '&title=' + encodeURIComponent('title of page');

Related

How get values variable javascript from razor?

i have soure code :
$("#yes-modal").click(function (e) {
var img = $("#url").val();
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = *value variable img* })';
});
i want get value img, but i have error.
How can do it ? thank you.
Below syntax should work.
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = ' + img + '})';
Let me know if you face any issue.
The razor syntax is a serverside code. It is evaluated while rendering the page from server. But the javascript is evaluated at the client side (browser). So you have to split it like below code.
$("#yes-modal").click(function (e) {
var img = $("#url").val();
var loc = '#Url.Action("DownloadImages", "Hrd")';
var loc += '?id=' + img;
window.location = loc;
});

Replaced %20 in URL with a +

I was wondering is it possible to replace the %20 to a + whenever I click the button to add the textbox value to the url.
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
url += $('#textbox').val();
window.location = url;
});
});
Thanks
Yep with a regex for all occurrences $('#textbox').val().replace(/%20/g, '+');
I haven't tested it but this should work. You want to replace the spaces in the value. They're not encoded to the entity at this point.
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
var textboxValue = $('#textbox').val();
textboxValue = textboxValue.replace(/ /g, "+");
url += textboxValue;
window.location = url;
});
});
Have you tried this?
$(function () {
$('#btn').click(function() {
var url = "www.urlname.com/results.html?value=";
url += $('#textbox').val();
window.location = url.replace(new RegExp(' ','g'),'+');
});
});
When you're creating URL parameters dynamically, you shouldn't use ad hoc substitutions to encode them. Javascript provides the encodeURIComponent function to perform all required encodings. So you should write:
url += encodeURIComponent($('#textbox').val());

jQuery mailto with anchor element

I tried this with umpteen examples we see on the net. But I guess there is none that is simple and works on all browsers (IE 8 and above as well).
I am trying to simply open up Outlook window with mailto link.
Email
JQuery:
$(function () {
$('#emailLink').on('click', function (event) {
alert("Huh");
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
});
});
Granted, I am a jQuery newbie. The above just doesn't work. It just flickers the browser but nothing opens. I guess this is because of window.location.
Is there a simple solution? I want this to work in IE8 & above and in all browsers.
The body is generated automatically (in JSP).
here's working solution:
Email
and the function:
$(function () {
$('#emailLink').on('click', function (event) {
event.preventDefault();
alert("Huh");
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
});
});
If you do not need the address as a text anywhere on the website I would suggest this:
$('a[data-mail]').on('click', function() {
window.location = 'mailto:' + $(this).data('mail')+'#yourdomain.net' + '?subject=Spotflow';
});
The link woud look like this:
Send me a mail
No chance for bots!
$(function () {
$('[name=emailLink]').click(function () {
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
$(this).attr('href', 'mailto:' + email +
'?subject=' + subject + '&body=' + emailBody);
});
});
.click can be replaced with .mousedown and so on.. or just
$(function () {
$('[name=emailLink]').each(function() {
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
$(this).attr('href', 'mailto:' + email +
'?subject=' + subject + '&body=' + emailBody);
});
});
Your selector is looking for an ID
$('#emailLink')
But you have only specified the name.
Add id="emaillink" to the anchor tag.
You don't need any javascript/jQuery at all for this, just the following HTML should do:
Email

How to get part of URL current?

My Url looks like: http://google.com/stackoverflow/post/1
Now, I want to get just part of URL: http://google.com/stackoverflow/ and add it to code:
Post . How to do it ? Thanks !
Plz try this:
var url = "http://google.com/stackoverflow/post/1";
var partOfUrl = url.split('post')[0];
Thanks.
UPDATED:
Using JavaScript:
var url = "http://google.com/stackoverflow/post/1"; // or var url = window.location;
var matches = url.match(/^http\:\/\/([\w\.]+)\/(\w+)\/.+/);
if( matches ){
var newURL = "http://" + matches[1] + "/" + matches[2] + "/";
alert( newURL );
}
document.getElementById('post_link').href = newURL;
HTML:
<a id="post_link">post</a>
See JSFiddle
use location Object
location.href
location.pathname

how to send mail using javascript?

I need to send a mail using javascript but i was unable to do it with
can any one please help with the code.
var link == "mailto:test#test.com"
+ "&subject=" + escape(":Feedback");
window.location.h ref = link;
You assign link variable with == which is not correct. Try with:
var link = "mailto:RAS.AMB#Wilhelmsen.com&subject=" + escape(":Feedback");
window.location.href = link;
Your javascript is not valid. You have a space in href and var link == should be var link =
This works.
var link = "mailto:RAS.AMB#Wilhelmsen.com&subject=" + escape(":Feedback");
window.location.href = link;

Categories

Resources