How to escape absolute URI? - javascript

In the script: `http://theip.com/something/index.php
I have the following javascript URI:
var uri = '/something/script.php?=' + someDynamicValue
That I pass to a function "loadHTML(url, div)"
someDynamicValue can contain spaces and other symbols which make JQuery crash with $.load().
So, I try to encode uri:
$('#'+div).load(encodeURIComponent(uri));
And gives
http://theip.com/something/%2Fsomething%2Fscript.php%3Fq%3D?_=1399924421585
That is, duplicating the /something (which should be an absolute URL so it should go to http://ip.com/something/script.php)
Now if I do the following:
$('#'+div).load(encodeURIComponent(uri).replace(/%2F/g,'/'));
I get a "good" url but gives 404 Error:
http://theip.com/something/script.php%3Fq%3D?_=1399923477529
So I guess it is taking script.php%3Fq%3D?_=1399923477529 as a literal script name, maybe.
How can I fix it? (Encode the rest of the URL).
Thanks!

You just need to encode the one part that isn't already properly URI encoded:
var uri = '/something/script.php?foo=' + encodeURIComponent(someDynamicValue)
$('#'+div).load(uri);

Related

request.getParameter returns dirty string

I have a site which is made by a CMS. When the site is visited as normal and a user search on the site with the URL:
https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV,coursesSV,lifeLongLearningCoursesSV&showbutton=false
The JS code:
let test = requester.getParameter("identity")
console.log(test)
// programSV,coursesSV,lifeLongLearningCoursesSV
returns programSV,coursesSV,lifeLongLearningCoursesSV as expected.
But when I visit the site at the exact same URL but I come from an external page JS code above returns this instead: programSV%2CcoursesSV%2ClifeLongLearningCoursesSV
Any suggestions on what could be wrong and how this could be fixed?
Use a URL search params for consistency - it will decode the URL for you regardless of encoded entities
Alternatively use decodeURIComponent
const url1 = new URL(`https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV,coursesSV,lifeLongLearningCoursesSV&showbutton=false`)
const url2 = new URL(`https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV%2CcoursesSV%2ClifeLongLearningCoursesSV&showbutton=false`)
console.log(url1.searchParams.get("identity"))
console.log(url2.searchParams.get("identity"))
// alternative
console.log(decodeURIComponent(`programSV%2CcoursesSV%2ClifeLongLearningCoursesSV`))
NOTE: decodeURI does nothing for your commas:
decodeURI(requester.getParameter("identity"))
returns programSV%2CcoursesSV%2ClifeLongLearningCoursesSV
however decodeURIComponent does work:
decodeURIComponent(requester.getParameter("identity"))
returns programSV,coursesSV,lifeLongLearningCoursesSV
the commas are being encoded, you have to use decodeURIComponent. Like this: decodeURIComponent(requester.getParameter("identity")). See link for reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent

how to download file using jquery in asp.net core?

I use this code to download file using jquery.
$('#dwnlod').click(function (ee) {
e.preventDefault();
var currentFile = $('#SlideContainer .actv').css('background-image').replace(/^url|[\(\)]/g, '');
alert(currentFile)
document.location.href = currentFile;
});
However, it always does nothing. the reason for this is when I remove e.preventDefault to read the URL I find it distorted.
it rather than the result from alert >>
"https://localhost:660066/Uploads/5c92f430-4aef-41c8-b201-853597935771.jpg"
it displays in a new browser tab >>
https://localhost:660066/Documents/Index/"https://localhost:660066/Uploads/5c92f430-4aef-41c8-b201-853597935771.jpg"
I don't know if I need extra steps with asp.net core routing !!?
It looks like the issue is that currentFile returns a string that is surrounded by quotes and thus the browser is interpreting it as a relative URL (since the scheme isn't at the beginning of the string). Removing the start and end quotes if they exist should get you the behavior you are looking for.

Uncaught URIError: URI malformed with reCAPTCHA

On some pages that have an encoded part in the url, like %FC instead of ü, I get a Uncaught URIError: URI malformed error in the console and the captcha therefore is not working.
Our Project is iso-8859-1 encoded and there is nothing I can change about that.
Do you know a workaround that could fix this? I'm using reCAPTCHA 2.
My solution now is, that before the recaptcha/api.js is executed, I grab the window.location.pathname, get rid of the problematic elements and exchange it in the url with history.pushState. If I'd simply changed the window.location.pathname a page redirect would be the consequence.
Here is my example code:
var oldPath = window.location.pathname;
var newPath = decodeURIComponent( unescape( unescape(oldPath)));
var stateObj = {};
history.pushState(stateObj, "", newPath);

Javascript: passing a URL to google encoding trouble

I'm trying to look at the html data returned from a google reverse image search. The URL I'm passing to google contains some characters that get encoded, notably ? changes to %3F, and google doesn't seem to understand the formatted URL (pic_url below). Is there any way to send the URL so that it does not get formatted? Or is there another way around this issue?
My code looks like:
var google_url = "https://www.google.com/searchbyimage?image_url=";
var pic_url = "http://img.cpcdn.com/recipes/_o1f2b886e/50x50c/d9e62798f1c807c1891454bed562e4c9.jpg?u=941483&p=1362455199";
var search_url = google_url + pic_url;
$.getJSON('http://whateverorigin.org/get?url=' +
encodeURIComponent(search_url) + '&callback=?',
function(data){
$("#targetWrapper").html(data.contents);
http_data = data["contents"];
console.log(http_data);
});
The error I get back in the console:
`The requested URL <code>/searchbyimage%3Fimage_url=http%253A%252F%252Fimg.cpcdn.com%252Frecipes%252F_o1f2b886e%252F50x50c%252Fd9e62798f1c807c1891454bed562e4c9.jpg%253Fu%253D941483%2526amp%253Bp%253D1362455199</code>
was not found on this server.
<ins>That’s all we know.</ins>`
But if you just copy and paste https://www.google.com/searchbyimage?image_url=http://img.cpcdn.com/recipes/_o1f2b886e/50x50c/d9e62798f1c807c1891454bed562e4c9.jpg?u=941483&p=1362455199 to the Address Bar it works. Any thoughts?
there is a bug in whateverorigin.org:
https://github.com/ripper234/Whatever-Origin/issues/1
note that the solution proposed doesn't work for me...

JavaScript bookmarklet and URL encoding

Fully disclosing that I do not know Javascript, I'm trying to get this Javascript:
javascript:location = 'http://validator.w3.org/check?uri=' +escape(location)&doctype=Inline&charset=detect+automatically&ss=1&group=0&user-agent=W3C_Validator/1.654';
to work as a Bookmarklet in order to send a URL of this format:
http://validator.w3.org/check?uri=http://www.wordpress.org&charset=%28detect+automatically%29&doctype=Inline&ss=1&group=0&user-agent=W3C_Validator%2F1.654
to the W3C valdiator.
I'm URL encoding the Javascript with this encoder, but of course, I'm doing something wrong, either in my Javascript or in the process of encoding it.
Anyone have some ideas in particular or in general about Javascript bookmarklets and URL encoding? Thanks.
Two Errors:
You need to access the "href" member of the location object:
window.location.href = http://foo.com
You have invalid JavaScript:
javascript:location = 'http://validator.w3.org/check?uri=' +escape(location)PLUS SIGN AND QUOTE MISSING HERE&doctype=Inline&charset=detect+automatically&ss=1&group=0&user-agent=W3C_Validator/1.654';
I recommend using this:
javascript:(function(){window.location.href='http://validator.w3.org/check?uri='+escape(window.location.href)+'&doctype=Inline&charset=detect+automatically&ss=1&group=0&user-agent=W3C_Validator/1.654';})()

Categories