passing veriable in url weird behaviour - javascript

I'm trying to pass a variable throught URL and it's doing something really weird in Chrome and Firefox, it seems is doing the same in OS.
The issue is that I'm trying to send a few variable in the URL this way:
document.location.href = 'http://www.ihaves.org/main.php?id='+facebookUser+'&first_name='+facebookName+'&last_name='+facebookName+'&locat='+locat_+'&locationlat='+facebookLat+'&locationlon='+facebookLon+'&mode=facebook';
The weird result is this:
http://www.ihaves.org/Vejer%20de%20la%20Frontera,%20Spain
when the expected result should be this one: (taken from IE)
http://www.ihaveseen.org/main.php?id=1215834998&first_name=Juanma%20De%20Los%20Santos&last_name=Juanma%20De%20Los%20Santos&locat=Vejer%20de%20la%20Frontera%20Spain&locationlat=36.25&locationlon=-5.96667&mode=facebook
I have detected that the problem is the variable called "locat", if I take it out then everything seems to go fantastic so I have thought that the comma of "Vejer de la Frontera, Spain" might be the problem so I have used:
var location_name = locationName.replace(/,/g, '');
But it keeps doing the same weird thing than before. Any idea? Does chrome accept an espace beetwen words? Remember firefox does exactly the same. By the way, I'm using GET_ in the PHP.

You should use encodeURI() on the Parameters when constructing the URL.
document.location.href = 'http://www.ihaves.org/main.php?id=' + encodeURIComponent(facebookUser);

Use:
encodeURIComponent(facebookName)
encodeURIComponent
You should be using encoded key/value pairs. Some characters will result in an invalid URL. For example:
mysite.com?name=Bed&Breakfast
will result in two values "name=Bed" and "Breakfast=" which is not what you would expect or want. Encoding would result in your URL:
mysite.com?name=Bed%26Breakfast
It may look funny but the URL is now valid and works as expected.

Related

Open Oracle Apex URL with JavaScript when giving parameters

when i use javascript eval() to open apex url from js i have no problem when i use eval() like this
eval("f?p=&APP_ID.:7:&SESSION.");
but when i wanna pass parameters with eval() like this
eval("f?p=&APP_ID.:7:&`SESSION.:P7_ID:8461,P7_ALLOWCHANGE:1,P7_WFDEF_ID:69004.");`
i get this error: SyntaxError: expected expression, got ':'
then these parameters automatically added after generating url
javascript:apex.navigation.dialog('f?p=101:7:28809985622510:::::\u0026p_dialog_cs=_7P7TVFV5LTQPjeyg-bGqSKpcYM',{title:'Workflow State',height:'auto',width:'720',maxWidth:'960',modal:true,dialog:null},'t-Dialog-page--standard '+'',this);:P7_ID:8461,P7_ALLOWCHANGE:1,P7_WFDEF_ID:69004.;
what should i do?
The best way would to be to use apex_page.get_url
It is so much simpler to use than apex_util.prepare_url
https://docs.oracle.com/cd/E59726_01/doc.50/e39149/apex_page.htm#AEAPI30190
I don't know about your eval call, but this APEX URL syntax is wrong:
f?p=&APP_ID.:7:&`SESSION.:P7_ID:8461,P7_ALLOWCHANGE:1,P7_WFDEF_ID:69004.
All the item names should be listed together, then all the values together - and after the correct number of colon separators:
f?p=&APP_ID.:7:&SESSION.::::P7_ID,P7_ALLOWCHANGE,P7_WFDEF_ID:8461,1,69004
I also removed the spurious back-tick character from before "SESSION".
Apart from what Tony Andrews covered, here are a few more issues with your URL:
1. it's APP_SESSION - not SESSION. Here's documentation on built in substitution strings.
2. Your items are not substituted properly. Read this documentation page more details on substitutions in APEX.
Here's documentation on understanding APEX URL syntax.
Secondly, here's what you would try. Create a hidden page item and use APEX_UTIL.PREPARE_URL function and generate valid url, assign to the item. And use that item as url in your javascript. I haven't tried this, but this would be a better approach, I think.
Also prepare url like this:
APEX_UTIL.PREPARE_URL('f?p=' || :APP_ID ||':7:' || :APP_SESSION || :::' ||:P7_ID: ',' || :P7_ALLOWCHANGE || ',' || :P7_WFDEF_ID || ':8461,1,69004')
Here's another great resource to understand apex url and how to pass variables:
http://dgielis.blogspot.in/2015/01/understanding-apex-url-passing.html

IE11 not accepting string(variable) as parameter to localStorage.setItem()

Does anyone knows why IE does not accept a string-variable as a parameter to the method setItem?
Works fine on Chrome.
Example on IE:
This works:
var itemName = 'anyname';
localStorage.setItem(itemName, 'anything');
This doesn´t:
var itemName = 'anyname';
var stringName = 'some string content';
localStorage.setItem(itemName, stringName );
This gives 'Invalid argument error'.
Any help on that?
Thanks! :)
EDIT.
That post relates about another problem, the example given in that post(the one that does not work for him) actually works for me!
My problem shows that the method setItem does not accept a string variable but accepts a normal enclosed quoted string.
Also the solution given in the related post is not acceptable to my problem, I can´t expect that the end user will install a IE11 bug-fix.
After heavy search, the problem lie in the contents of your string. IE11 setItem method does not accept certain chars. My original string´s contents had things like '|' and '~'.
The only workaround I find is to use the encodeURI(yourStringHere) before sending it to the setItem method and after decodeURI it.

"%" is getting URI decoded while everyhting else not

I have a strange UI5 problem. I create a string from a control's binding context which looks like:
Entity('Element%3AInfo%2CID')
Just for info, it looks like this decoded: Entity('Element:Info,ID')
However, I get this String from the following method chain:
oItem.getBindingContext().getPath().substr(1)
So, the whole (pretty basic) "navigate to" block looks like this:
showElement : function (oItem) {
'use strict';
var bReplace = jQuery.device.is.phone ? false : true;
sap.ui.core.UIComponent.getRouterFor(this).navTo("element", {
from: "master",
element: oItem.getBindingContext().getPath().substr(1),
otherpattern: "something"
}, bReplace);
},
A console log in this block console.log(oItem.getBindingContext().getPath().substr(1)); provides the right string.
Console output of console.log(oItem.getBindingContext().getPath().substr(1)):
Entity('Element%3AInfo%2CID')
The problem is (be aware, this is getting curious) that my URL pattern "{element}" is filled with:
Entity('Element%253AInfo%252CID')
Decoded: Entity('Element%3AInfo%2CID')
As you probably already know, the pattern's "%" is encoded. I don't get why UI5 would do this.
You should also know these facts which I've tested:
decodeURIComponent(oItem.getBindingContext().getPath().substr(1)) leads to "Entity('Element:Info,ID')"
encodeURIComponent(oItem.getBindingContext().getPath().substr(1)) leads to "Entity('Element%25253AInfo%25252CID')"
oItem.getBindingContext().getPath().substr(1).replace("%3A", ":") leads to "Entity('Element:Info%252CID')"
Is this a bug? I mean the URI pattern is left untouched as long as it doesn't come to a "%". For some odd reason this special character is encoded while everything else doesn't matter.
Its not exactly like "%" is getting encoded and everything else is not encoded.
I also came across this issue. SAPUI5 does encoding once, and browser does it second time. Hence in the second iteration you will have only "%" to be encoded.
Initial string : Element:Info,ID
after first iteration of encoding(by UI5 framework) encodeURIComponent('Element:Info,ID') : We get Element%3AInfo%2CID
So for the second iteration, only % is left to be encoded Element%253AInfo%252CID hence we get this.
So if you are picking up the binding context from URL, you need to decode twice.
Else as you are doing once is fine.

How to get anything following the domain in a url, using Javascript

What is the best way to get the "anything" part after the domain part, using Javascript:
http://www.domain.com/anything
http://www.domain.com/#anything
http://www.domain.com/any/thing
For http://www.domain.com/#anything I would have to use window.location.hash. But for http://www.domain.com/anything I would have to use window.location.pathname.
I'm using:
window.location.href.replace(window.location.origin, "").slice(1)
Are there any caveats with this solution? Is there a better way?
Caveats:
location.origin is not supported by IE.
Other improvements: .slice is actually calling Array.prototype.slice. A method call that requires a prototype lookup is bound to be slower than accessing the element you need directly, escpeciallly in your case, where the slice method is returning an array with just 1 element anyway. So:
You could use location.pathname, but be weary: the standard reads:
pathname
This attribute represents the path component of the Location's URI which consists of everything after the host and port up to and excluding the first question mark (?) or hash mark (#).
but I think the easiest, most X-browser way of getting what you want is actually simply doing this:
var queryString = location.href.split(location.host)[1];
//optionally removing the leading `/`
var queryString = location.href.split(location.host)[1].replace(/^\//,'');
It's very similar to what you have now, except for the fact that I'm not using location.origin, which, as shown on MDN is not supported by MS's IE...
Another benefit is that I'm not calling Array.prototype.slice, which returns an array, and requires a prototype-lookup, which is marginally slower, too...
window.location.pathname + window.location.search + window.location.hash
I think this one is a little bit better. You dont have to use any functions here...

Parse URL value and remove anything after the last '/'

My website URL is (example) http://mypage.com/en/?site=main. Then comes JavaScript code that, together with PHP, parses the data.
After that, I need some code that will change the URL inside the adress bar to http://mypage.com/en/, that is, removes the stuff after the last / (slash).
If possible, it is should be jQuery/JavaScript code.
I found something that will work.
You have to use a method called replaceState().
Mozilla developer reference
var str = window.location.href;
window.history.replaceState({}, '', str.substr(0,str.lastIndexOf("/"))+"/");
Use split() function in javascript.
Example :
var url = "http://mypage.com/en/?site=main";
alert(url.split('?')[0]);

Categories

Resources