Here is my problem:
window.location.href = "(X(1)S(" + "#Session.SessionID" + "))/Cart/AddToCart?productID=" + "#Model.ProductID";
Basically on click of <p> tag I want to call this link with sessionID in the url. The problem is it either doubles the sessionID part or it adds the whole string at the end of current url. When I remove this part "(X(1)S(" + "#Session.SessionID" + ")) everything works fine. Any ideas why it's doing that?
Solution:
I have no idea why did I get two negative votes, but the issue was, I needed a '/' before the session ID. That's it!
Depending on how your session handler is passed, it may get appended automatically to any URL, so that you don't have to do it yourself. Since you're doing it yourself anyway, it gets doubled up by the automatic one.
Because you set window.location.href to an inappropriate value. It should be set to a proper URI, which usually don't look like (X(!)S(... but start with http://...
Related
Title says it all, I would like to trigger a jQuery event that opens a unique model window depending on which URL is used. I've looked at a few solutions and all of them seem to require bootstrap which I am not using or simply don't seem to work for me.
I think i understand the logic, I'm just not sure how to actually code it and would be grateful for some help. Here is my thinking:
[STEP 1]
On page load, check the URL.
If the the url is normal e.g. "www.domain.com/example", don't do anything.
If the url has a substring on the end e.g. "www.domain.com/example/#red", "www.domain.com/example/#green", or "www.domain.com/example/#blue" etc., set that substring to a variable. In this case the variable would equal either red, green, or blue.
[STEP 2]
Insert the variable where the line of code says [color] and execute.
$("document").ready(function() {
$(".details, #[color]details").trigger('click');
});
use with window.location.hash. its will get the hash value form url with# .so no need to add # in the dom
$("document").ready(function() {
if(window.location.hash.trim().match(/(\w+)/)){
$(".details,"+window.location.hash+"details").trigger('click');
}
});
You can use document.referrer to get the Page URL.
Store it in a variable to fetch the last segment or the URL using substr().
Then check it in conditional operator if the last part is your desired text, add it to your class and trigger.
I ll paste my code which I used on the next page to trigger tab change for some requirement. I hope this will work for you too, hopefully. Thank you.
$(document).ready(function () {
var referrer = document.referrer; // Get the Url of the previous page
var lastPathSegment = referrer.substr(referrer.lastIndexOf('/') + 1); // extracts the last part e.g. the page name
if(lastPathSegment == "invoices.php"){
customer_detail_content();
$('a[href="#tab_6_2"]').trigger('click');
}
User inputs a web address that I want to get only the tail from, as I do know what site he inputs.
So first I want to remove the "main" URL and get what ever is at the end, so my action is:
Original link: http://example.com/something
var n=e.split("http://example.com/");e=n[1];
And I will get "something"
The problem is that site can also be secured, thus having https not http. Therefore the split wont work.
How do I define a split function, that would work like this:
split("http://example.com/ || https://example.com/")
I do not want to split by looking at "//" or anything of that sort, I want an exact address.
If you like it clear and want to avoid regular expressions, try this:
var n=e.split("http://example.com/",2).pop().split("https://example.com/",2).pop();
If you wish to know the host you can do so by using this code instead in JavaScript:
window.location.host
Source Get The Current Domain Name With Javascript (Not the path, etc.)
You can also use window.location.path to get the URL that was requested, combining those you get:
window.location.host + window.location.pathname
For me, this outputs stackoverflow.com/posts/25203020/edit while writing this reply.
var s = "http://example.com/something";
function split (url) {
var r = /([^:]+):\/\/([^\/]+)\/(.*)/gi;
var a = r.exec(url)
return [a[1], a[2], a[3]];
}
I have the following javascript which works fine for the most part. It gets the user that has logged in to the site and returns their DOMAIN\username info. The problem arises when the username starts with a letter that completes a valid escape character (eg. DOMAIN\fname). The \f gets interpolated and all kinds of terrible things happen. I have tried every sort of workaround to try and replace and/or escape/encode the '\'. The problem is the \f does not seem like it is available to process/match against. The string that gets operated on is 'DOMAINname'
// DOMAIN\myusername - this works fine
// DOMAIN\fusername - fails
var userName='<%= Request.ServerVariables("LOGON_USER")%>';
userName = userName.replace('DOMAIN','');
alert("Username: " + userName);
I also see all kinds of weird behaviour if I try to do a workaround using the userName variable, I think this may be because it contains a hidden \f. I've searched high and low for a solution, can't find a thing. Tried to find out if I could remove the DOMAIN\ on the serverside but that doesn't seem available either. Does anyone have a solution or workaround? In the debugger, the initial value of the servervariable is correct but the next immediate call to that variable is wrong. So the interpolated values in the debugger look like this:
var userName='DOMAIN\fusername';
userName; // 'DOMAINusername' in debugger.
Thanks
If you're using ASP.net (as it looks like you are), use AjaxHelper.JavaScriptStringEncode or HttpUtility.JavaScriptStringEncode to output the string correctly.
var userName='<%= HttpUtility.JavaScriptStringEncode(Request.ServerVariables("LOGON_USER"))%>';
I need to remove the domain name from location.href using Javascript. I have links like: http://localhost/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO and I need to have links without http://localhost and in future without it's real domain name.
I will use those trimed links in Javascript function so I would like to trim it also in Javascript.
I have tried: window.location.href.split('/')[2]; but I could only get domain form it. And I want to get rid of domain.
Any help here much appreciated!
Use window.location.pathname. This gives you the path relative to the host. See here for more details.
For any arbitrary URL, assuming that the variable url contains your URL, you can do:
url = url.replace(/^.*\/\/[^\/]+/, '')
Rather than doing string manipulation on window.location.href, you can use the other properties of window.location. In your case, you want the pathname, the search and the hash:
console.log(window.location.pathname + window.location.search + window.location.hash);
I posted this on your other question as a comment but I might as well add it here too. You can use a replace with a regex, like this:
location.href.replace(/.*\/\/[^\/]*/, '')
Things have changed a bit since 2011 when the original answer was posted. Now you can use the URL Web API.
https://developer.mozilla.org/en-US/docs/Web/API/URL
For your question, the following code:
let dummy_url = new URL("http://localhost/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO");
console.log( "pathname : " + dummy_url.pathname );
console.log( "search : " + dummy_url.search );
console.log( "both : " + dummy_url.pathname + dummy_url.search );
Generates the following output.
pathname : /App/User/UserOrder.aspx
search : ?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO
both : /App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO
The combined pathname and search fields is what you want.
As of Nov 2021 this is available in everything except IE and Opera Mini.
Try this:
window.location.pathname
Fiddle: http://jsfiddle.net/maniator/zKruK/
I'm using document.location.hash to preserve state on the page, and I'm putting url-encoded key value pairs up there, separated by "&" chars. So far so good.
However I'm running into an annoying problem on Firefox -- Firefox will quietly url-decode the hash value on the way in, so when you get it out later it's been decoded.
I can patch the problem by detecting when I'm running on firefox and calling encodeURIComponent on everything twice on the way in, but obviously that is hideous and I don't really want to do that.
Here's a simple example, where I encode "=" as "%3D", put it in the hash, and when I get it out later it's been turned back into "=" automatically:
// on the way in::
document.location.hash = "foo=" + encodeURIComponent("noisy=input");
//then later.....
// on the way out:
var hash = document.location.hash;
kvPair = hash.split("=");
if (kvPair.length==2) {
console.log("that is correct.")
} else if (kvPair.length==3) {
console.log("oh hai firefox, this is incorrect")
}
I have my fingers crossed that there's maybe some hidden DOM element that firefox creates that represents the actual (un-decoded) hash value?
but bottom line -- has anyone run into this and found a better solution than just doing browser detection and calling encodeURIComponent twice on Firefox?
NOTE: several other questions I think have the same root cause. Most notably this one:
https://stackoverflow.com/questions/4834609/malformed-uri-in-firefox-not-ie-using-encodeuricomponenet-and-setting-hash
I would strongly advise against using the hash value to preserve the state. Hash is supposed to point to object's fragment-id, as explained in RFC 1630
This represents a part of, fragment of, or a sub-function within, an
object. (...) The fragment-id follows the URL of the whole object from which it is
separated by a hash sign (#).
Is there anything stopping you from using cookies to preserve the state? Cookies are simple enough to use in JS, described on Geko DOM Reference pages, and would do the trick quietly, without appending values to the URL which is never pretty.
If you absolutely have to use hash though, you may want to consider replacing '=' with some other character, e.g. ":".
What you could do, is change the "=" to something else using
var string = string2.replace("=", "[$equals]")
You may have to run the line above a couple of times, depending on how many "=" there are.
Then same process you had as above.
NB If you require it for further code, you can replace [$equals] back to "=" after splitting the hash into an array.