I'm trying to get the url parameter(device) and call it out in the body. the code below works great except it'll come out as iPhone+4s*. Is there any way to remove the + and * and just have it say iPhone 4s?
url - http://mydomain.com/click.php?device=[[device_name]]
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || ''
);
}
</script>
<body>
<script>document.write(getURLParameter('device'))</script>
</body>
You have to just replace all & + = because they aren't going to decode as they are allowed characters in url-encoding.
uri = decodeURIComponent(uri);
uri = uri.replace(/\+/g, '-');
Related
Existing Url
http://example.com/home.php?id=1&branch_id=4&course_id=5
New url
http://example.com/home.php?id=1&branch_id=4
removing course_id from existing url
How to remove one parameter value from url.
Tried below code:
function replaceUrlParam(url, paramName, paramValue){
var pattern = new RegExp('(\\?|\\&)('+paramName+'=).*?(&|$)')
var newUrl=url
if(url.search(pattern)>=0){
newUrl = url.replace(pattern,'$1$2' + paramValue + '$3');
}
else{
newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue
}
return newUrl
}
but its not working...
As you said that you want to use variable instead of directly using .replace(/&course_id=\d+/, ''), you can do it like below:-
Example:-
var re = new RegExp("&course_id=\\d+");
var newUrl="http://example.com/home.php?id=1&branch_id=4&course_id=5";
console.log(newUrl.replace(re, ''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You can simply do that by using this code to remove &course_id=5:
var url = "http://example.com/home.php?id=1&branch_id=4&course_id=5";
url.replace(/([&\?]course_id=5*$|course_id=5&|[?&]course_id=5(?=#))/, '');
It will return you: http://example.com/home.php?id=1&branch_id=4
I know it's already discussed here, but there were no solution to get the whole document (including doctype).
$(document).html(); returns null...
This will get you all the HTML:
document.documentElement.outerHTML
Unfortunately it does not return the doctype. But you can use document.doctype to get it and glue the two together.
You can do
new XMLSerializer().serializeToString(document);
for all browsers newer than IE 9
try this.
$("html").html()
document is a variable it dose not represent the html tag.
EDIT
To get the doctype one could use
document.doctype
This is a function which has support in IE6+, it does't use outerHTML for even more support, it adds the doctype and uses a few tricks to get the html tag and its attributes. In order to receive a string with the doctype, and doesn't use outerHTML so it supports every browser. It uses a few tricks to get the html tag. Add this code:
document.fullHTML = function () {
var r = document.documentElement.innerHTML, t = document.documentElement.attributes, i = 0, l = '',
d = '<!DOCTYPE ' + document.doctype.name + (document.doctype.publicId ? ' PUBLIC "' + document.doctype.publicId + '"' : '') + (!document.doctype.publicId && document.doctype.systemId ? ' SYSTEM' : '') + (document.doctype.systemId ? ' "' + document.doctype.systemId + '"' : '') + '>';
for (; i < t.length; i += 1) l += ' ' + t[i].name + '="' + t[i].value + '"';
return d+'\n<html' + l + '>' + r + '</html>';
}
Now, you can run this function:
console.log(document.fullHTML());
This will return the HTML and doctype.
I ran this on example.com, here are the results
document.documentElement.innerHTML
will return you all document markup as string
to get the whole doctype read this
I'm not sure about getting the complete doc.but what you can do is,you can get the content of html tag seprately and doctype seprately.
$('html').html() for content and document.doctype for getting the doctype
I don't think there is a direct access to the whole document (including the doctype), but this works :
$.get(document.location, function(html) {
// use html (which is the complete source code, including the doctype)
});
I have done it on browser's console
document.documentElement;
I am trying to insert an email link using jquery, but if the email body has a & in it, it will not work.
$('#em_button').html('<img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" />');
The part that is breaking it is &img=. I've tried using & but that didnt work either. Any suggestions?
Note: If there is no &, everything works fine.
You can try this, hope it works
'%26img='
Without knowing what is in js_images[getImg], it's hard to say what is going wrong.
One thing to keep in mind is that you should always encode components before creating a URL. For example:
var x = encodeURIComponent(js_images[getImg].title)
$('#em_button').html('<a href="mailto:?subject=A Photo: '+ x +'&body...')
Furthermore, if you're creating something like a link, you might want to make things clearer by writing it as follows:
var url = 'mailto:subject=A Photo: ' + encodeURIComponent(js_images[getImg].title) +
'&body=I thought you would like this photo: ' + encodeURIComponent(thisPage) +
'&id=' + encodeURIComponent(getGall) +
'&img=' + encodeURIComponent(js_images[getImg].filename);
var src = encodeURIComponent(homeUrl) + '/' +
encodeURIComponent(tUrl) + '/img/email.png'
var $a = $('<a/>', {
href: url
});
var $img = $('<img/>', {
src: src,
title: 'Email this photo'
});
$a.append($img);
$('#em_button').html($a);
$('#em_button').html('<img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" />');
If you just append the URL parameters and the body text into one string, the & from inside the body text would be parsed as URL argument. In PHP i would use urlencode to encode occurrences of such characters into %xx-values.
Looking at the answer of Константин Рекунов, I would assume you could use encodeURIComponent to encode the body contents.
encodeURIComponent( thisPage + '?id=' + getGall + '&img=' + js_images[getImg].filename )
See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
You could avoid that problem by using URL rewriting on your server for the image files.
Instead of:
thisPage + '?id=' + getGall + '&img=' + js_images[getImg].filename
…it might be:
thisPage + '/' + getGall + '/img/' + js_images[getImg].filename
I think your problem is you have a second ? in your query string.
You should substitute the second ? with &. Also all your & should be written as &
$('#em_button').html('<img src="' + homeUrl + '/' + tUrl + '/img/email.png" title="Email this photo" />');
Instead of using "&" try using &
use +encodeURIComponent('&')+
I have a C# ASP.NET app that creates a JavaScript array of values for some user profile information. Client-side, I use jQuery/JavaScript to read the array and generate a mailto link. Some of the fields can contain special characters, such as ' & = / \ ".
Here's the C# code:
private String generateElementsArray(){
StringBuilder sb = new StringBuilder();
sb.Append("var currentElements = { currentUserName: '");
sb.Append(currentUserName);
sb.Append("', currentUserEmail: '");
sb.Append(currentUserEmail);
sb.Append("', currentSite: '");
sb.Append(currentSite);
sb.Append("', currentTitle: '");
sb.Append(currentTitle);
sb.Append("'}");
return sb.ToString();
}
I write the value of the above method to the page, which produces this JavaScript:
<script type="text/javascript">var currentElements = { currentUserName: 'Alex', currentUserEmail: 'myemailID', currentSite: 'Helpdesk', currentTitle: 'Phone User Guides & Troubleshooting'}</script>
Then I generate the email link using this JavaScript code, attaching the anchor tag to an element on the page:
function generateEmailTo(){
var body = currentElements.currentUserName + ' has shared a page with you on the intranet.%0A%0APage Title: %22' +
currentElements.currentTitle.replace("&","and") + '%22%0A' + $(location).attr('href').replace('#','');
var subject = currentElements.currentUserName + ' has shared a page with you on the intranet';
var mailto = 'mailto: ?body=' + body + '&subject=' + subject;
var anchor = '';
$("#email-placeholder").wrap(anchor);
}
....
<img id="email-placeholder" title="Send this page to a friend." src="<%= baseUrl %>/SiteAssets/media/icons/email-icon.gif"/>
For the mailto body text, I've noticed that it only takes a small set of the encoded characters, such as %22 for double-quotes, and %0A for line breaks. How do I pass the special characters such as single quotes, ampersands, etc., to the mailto body text and keep JavaScript happy?
Mailto is a URI scheme so all of its components must be URI encoded. You can use JavaScript encodeURIComponent function to encode mailto components. Please see the following example:
function buildMailTo(address, subject, body) {
var strMail = 'mailto:' + encodeURIComponent(address)
+ '?subject=' + encodeURIComponent(subject)
+ '&body=' + encodeURIComponent(body);
return strMail;
}
var strTest = buildMailTo('abc#xyz.com', 'Foo&foo', 'Bar\nBar');
/* strTest should be "mailto:abc%40xyz.com?subject=Foo%26foo&body=Bar%0ABar" */
window.open(strTest);
Hope this help.
I've replaced the submit URL from the search form with this jQuery snippet:
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
window.location.href = "/search-" + $('.search-form input:text').val() + "-" + "keyword"+ "-"+ "keyword2/" + $('.search-form input:text').val() + ".html";
return false;
});
});
</script>
This works fine and turns the URL into a nice SEO cleaned URL. But how can I replace the spaces?
When someone types in "search me" the URL looks like /search-search me-keyword-keyword2/search me.html with spaces. With + or - it would look much better. I know of str_replace from PHP, but how would I go about this in jQuery?
There's a native JavaScript function called encodeURIComponent that's intended to do exactly what you need.
window.location.href =
"/search-" +
encodeURIComponent($('.search-form input:text').val()) +
"-" + "keyword" + "-" + "keyword2/" +
encodeURIComponent($('.search-form input:text').val()) +
".html";
Method 1: Using Replace
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
var value = $('.search-form input:text').val();
value = value.replace(' ', ''); // replace
window.location.href = "/search-" + value + "-" + "keyword"+ "-"+ "keyword2/" + value + ".html";
return false;
});
});
</script>
Method 2: Encoding URL
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
// encode url
var value = encodeURIComponent($('.search-form input:text').val());
window.location.href = "/search-" + value + "-" + "keyword"+ "-"+ "keyword2/" + value + ".html";
return false;
});
});
</script>
Note that replace method would work even in JQuery because Jquery is simply library of javascript :)
http://www.w3schools.com/jsref/jsref_replace.asp
replace() method is native javascript. That'll get you what you want.
You could remove spaces with using mod_rewrite. It’s quite useful way. You can also remove spaces from URLs by replacing spaces by %20, but that only helps with spaces and won't escape other characters. What you really need to do is use a URL-escaping function.