Javascript split ain't working on iPad (safari) - javascript

Javascript ain't working for me ;-(
function myFunction() {
var url = document.getElementById("value1").value;
document.write(url.split('?')[0]);
window.location = 'http://example.com/?s='+ url.split('?')[0];
}
value1 = https://www.xxxxxxxxx.com/nl/p/yyyyyyyyyyyyyy/9200000056577975/?suggestionType=browse
i only need the stuff before the questionmark (https://www.xxxxxxxxx.com/nl/p/yyyyyyyyyyyyyy/9200000056577975/)
it is working on PC (IE, Opera,...), but it ain't on iPad (safari)
I get this on ipad: https://www.xxxxxxxxx.com/nl/p/yyyyyyyyyyyyyy/9200000056577975/suggestionType=browse
What is wrong?
update:
I have added some code:
function myFunction() {
var url = document.getElementById("value1").value;
document.write(url.split('?')[0]);
url = url.replace(/%2F/g,'/');
url = url.replace(/%3A/g,'/');
url = url.replace(/%3F/g,'?');
window.location = 'http://example.com/?s=' + url.split('?')[0];
}
it is working but safari is screwing up the urls that are added. Input box is used for urls. on PC all working as it should. the .replace are used to try edit the submitted urls but ain't working. Safari document.write still shows the right urls without the %2F and %3A .

Related

Combine window.location.href & document.referrer into a single variable

I need to get the current web page url in a single variable like:
var x = window.location.href;
But the problem is the script will be used sometimes in Cross domain Iframe & sometimes in same domain. Since window.location.href does not work in Cross domain iframe, so I have tested
x = document.referrer for cross domain Iframe(read only) and it worked fine. But I need both option in a single variable so I can use it in url validation with .match() method in Javascript.
I tried to combine like this, but its not working
var x = window.location.href;
if (typeof(window.location.href) === 'undefined') {
x = document.referrer;
}
My full codes are here:
// Get the url-This part isn't working
var x = window.location.href;
if (typeof(window.location.href) === 'undefined') {
x = document.referrer;
}
// Url validation--This part is working fine
var y = "zebra";
var rgxp = new RegExp(y, "gi");
if (x.match(rgxp) > -1) {
alert('Found a zebra in url');
I don't know is it really possible to combine or not, but otherwise I think I have to repeat this script twice. 1 for cross domain Iframe & a 2nd for the same domain.
I am fairly new in Javascript & got stuck here, so any help and direction would be a lifesaver

Will 'http:url' work for all browsers and devices?

In making a function that validates a user URL and prepends http: at the front, I have to take cases of www, https and // into account as being valid urls. The way I have it written now (see below), I only prepend http: , so that cases of //stackoverflow.com don't turn into http: ////stackoverflow.com.
This means that a url like stackoverflow.com becomes http:stackoverflow.com.
In Firefox and Chrome, this works just fine, but these URLS will be clicked from a variety of browsers and devices. Is it something that'll work universally? It'll be easy to rewrite this check for a // case, but I'm interested in the answer.
Prepend method:
function prependHTTPtoWebURL() {
var url = (el('org_website').value);
var httpVar;
var testFor;
if (url) {// If there's a website URL value
testFor = url.toLowerCase();
if (testFor.indexOf("http") != 0){
httpVar = 'http:'; //add it
url = httpVar + url;
el('org_website').value = url;
}
}
}
Try playing with regex. Check this code for instance:
var someurl = "www.google.com";
var otherurl = "google.com";
var anotherurl = "//google.com";
function prependHTTPtoWebURL(url) {
var newurl = url.replace(/^(http)?(:)?(\/\/)?/i,'');
return 'http://' + newurl;
}
console.log(prependHTTPtoWebURL(someurl));
console.log(prependHTTPtoWebURL(otherurl));
console.log(prependHTTPtoWebURL(anotherurl));
The ouput in console.log will be:
http://www.google.com
http://google.com
http://google.com
Since you are specifying a subdomain (www) on the first one, that is respected. It avoids ending with four diagonals, like http:////. If your url was something like :google.com, it would also fix it correctly.
You can see it live here: http://jsfiddle.net/zRBUj/
Edit: Adding the /i Kate mentioned.
Change http: to http://
See these links for more info:
Anatomy of a URL
How the web works

IE javascript troubles

I have site which was mainly designed for full flash with SWFAddress, therefore all links end up with hash "#" sign (eg.: www.site.com/#/folder/image.jpg).
I want to switch flash to full HTML and don't make any of the links dead (as I left a lot of links around the web), so I created htaccess file to control the incomming requests.
The problem was with the hash "#" sign because it couldn't be handled by htaccess file. I made a piece of javascript code on top of my first index.php.
So, before I load any controller, the index.php file starts with javascript to remove hash sign and redirect back to the site without "#" i n url :
<script type="text/javascript">
var link = location.href;
m = removeHash(link);
if (link != m) {
document.location.href = m;
}
function removeHash(l) {
var unhashedUrl = l;
if (l.indexOf("#") > -1) {
var url_pieces = l.split('/');
var request = url_pieces[url_pieces.length-1];
if(request.indexOf("#") == -1 && request.toLowerCase().indexOf(".jpg") > -1){
unhashedUrl = l.replace('#', "gallery");
}else{
unhashedUrl = l.replace('#/', "");
}
}
return unhashedUrl;
}
</script>
Then all php stuff begins...
Chrome, firefox, Opera, Safari, mobile browsers... They all works flawlessly. But again the IE (tested IE8 and IE9) end up crazy. It looks like IE won't fire any JS later on. Of course when I remove JS from index controller IE works good too.
Any advice how should I achieve this?

Window.Location Not Working In IE?

I have been trying to figure this out all afternoon, but have given up and now turning to you clever people to help :)
I have the following Jquery/Javascript function, which is working fine in Chrome - BUT in IE nothing happens?
$(".btnsubmitpost").click(function () {
var topicid = $(this).attr('rel');
var sbody = tinyMCE.get('txtPost').getContent();
$('.topicpostlistnewpost').remove();
$('.postsuccess').show();
$.post("/myurl/" + topicid + ".aspx",
{ "postcontent": sbody },
function (data) {
var returnUrl = $("value", data).text();
window.location.href = returnUrl;
return false;
});
return false;
});
I have tried window.location, window.location.href both with full urls and absolute Urls but IE just doesn't like it? Any ideas?
The function just gets a Url back from a post, and is supposed to redirect the user to the Url. But like I say, works in Chrome fine just not in IE (Tried IE8 and IE9)
Just for anyone having the same issue, the problem was because the window.location was inside the Ajax post method.
Just replace the window.location with a function() that then calls the window.location or do it after the Ajax call completely.

javascript page refresh in Safari

I am trying figure out how to refresh page in Safari (5.1) using javascript and nothing seems to work.
So far, I have tried,
window.location.href = window.location.href
window.location = window.location.href
window.location.reload(true)
window.location.replace(window.location.href)
What is the right way of handling page refresh in Safari?
Apparently, Safari on Mac or iOS has a bug with location.reload, so, I've developed this simple cross browser solution taking advantage of the url query string:
function refresh() {
var url = location.origin;
var pathname = location.pathname;
var hash = location.hash;
location = url + pathname + '?application_refresh=' + (Math.random() * 100000) + hash;
}
location.reload(true); // works for safari
If you didn't know already this site, let have a look on it, you will have a lot of example for refreshing page: http://www.quackit.com/javascript/javascript_refresh_page.cfm
You should always use the reload() method from the location object...
window.location.reload();
Set the first argument to true if you want to hard reload (send a new GET request for the page).

Categories