How to check if current URL ends in a specific pattern - javascript

How do I check if the current URL ends in: #!/about or #!/ask using JavaScript?

window.location.hash contains the current hash code, so basically:
if(window.location.hash === '#!/about') {
// Do something
} else if(window.location.hash === '#!/ask') {
// Do something else
}
You can use it however you need to.
Edit: As zzzzBov points out, if you need some jQuery, put it in a $(document).ready handler. ;)

To fetch the current url using jQuery
$(location).attr('href');
Here's the code snippet
jQuery(document).ready(function() {
var url=$(location).attr('href');
var match1 = arr.match('#!/about');
var match2 = arr.match('#!/ask');
if(match1){
alert("match1 found");
} else if{
alert("match2 found");
}
});

Related

I want access address bar content in JavaScript

I'm trying to route to a dynamic page in JavaScript,
Is there any way I can do this,
localhost/page.html/001
Can I write a code like,
If (url last characters == 001) {
//Do something
}
<script type="text/javascript">
var lastUrl = window.location;
lastUrl = lastUrl.replace("localhost/page.html/", "");
if(lastUrl == "001"){
alert(lastUrl);
}
</script>
You could use:
const currentUrl = window.location.href
To get the current URL of the side (In this case localhost/page.html/001), then:
const filterUrl = currentURL.split("/");
if (filterUrl[2] === '001') { /* Do stuff */ } else { /* Do other stuff */ }
If your URL's are going to be always like that you could use this little snippet to filter them.

How to check window.location exist or not

My current code is working when location is found. But I want to make a check that if location is not found, it will redirect somewhere else.
Here is my code:
<script type="text/javascript">
var userAgent = window.navigator.userAgent;
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
window.location = "theMYApp://"
}
else
{
}
</script>
I want to check if window.location = "theMYApp://" does not exist
If you want to check whether window.location object is existed on a web browser, you do not check. window.location object is always existed.
If you want to check text of 'theMYApp://' in window.location object, you can use below codes.
if (window.location.indexOf('theMYApp://') > -1) {
// theMYApp:// string is found in url
}

How to check in # in url?

Sometimes because of cache I add # in my url, like this:
http://www.example.com/#lang=3201954253
What I need is to check if there is #lang in url and remove it if present.
you can clear the hash.
window.location.hash = '';
or you can even use history api History Api. history.pushState and replaceState
history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.
window.history.replaceState( {} , 'foo', '/foo' );
You may try like this:
if(window.location.hash) {
// code
} else {
// code
}
or you may try this:
<script type="text/javascript">
if (location.href.indexOf("#") != -1) {
//code
}
</script>
If you want to remove it then you may try this:
window.location.hash = ''
On a side note:
You may try
window.location.href.split('#')[0]
to remove anything after # without refreshing your page.
if (window.location.hash.indexOf("lang")) {
window.location.hash = "";
}
var tel = window.location.href;
if(tel.indexOf("#") > -1){
alert("found");
} else {
alert('not found')
}

window.location.assign(varString) wont work

I have java script function to check the URL and split it,
I ask a question and depends on the answer it will forward the user page
all works fine until I use window.location.assign();
with string inside (=window.location.assign(path);) instead of fixed URL (=window.location.assign("http://stackoverflow.com");)
what can i do?
thanks...
var register=...;
var login=...;
function link(type) {
var urlPath = document.URL.split("/");
if (type == "register") {
var path= urlPath[2] + register;
window.location.assign(path);
}
else {
var path = urlPath[2] + login;
window.location.assign(path);
}
event.preventDefault();
}
You should use the full URL.
window.location.assign(urlPath[0]+'/'+urlPath[1]+'/'+urlPath[2]+register);
window.location.assign(urlPath[0]+'/'+urlPath[1]+'/'+urlPath[2]+path);
Or
window.location.assign(window.location.origin+register);
window.location.assign(window.location.origin+path);

window.location.hash returns hash tag in front of value

I have the following code in MVC3 view:
$(document).ready(function () {
if (window.location.hash) {
var manager= new Manager();
manager.doSomeStuff(window.location.hash);
}
});
The interesting thing is that when there is no hash tag in the URL, or there is only a hash tag example:
http://localhost:1223/Index/AboutUs
http://localhost:1223/Index/AboutUs#
When the window.location.hash is empty and the function is not executed.
But when there is some value in the hash tag:
http://localhost:1223/Index/AboutUs#categoryId=5&manufacturerId=8
The value in the window.location.hash is #categoryId=5&manufacturerId=8
Can you explain to me why the # tag is included in the value and why when there is no value after the # tag, the window.location.hash is empty.
There's nothing much to explain. It is the way it works.
Read more here: http://www.w3schools.com/jsref/prop_loc_hash.asp
Definition and Usage
The hash property returns the anchor portion of a URL, including the hash sign (#).
You can change it if you want by simply changing the hash name:
//Your old hash name caught in a variable
var nameHash = location.hash;
//Your new hash name without "#"
var newHashName = nameHash.replace("#","");
var hash = window.location.hash.substring(1);
This omits the first character of the string, which is the hash tag.
You can repalce # but this way will create conflict and won't work with javascript.
Here is window.location reference link.
Here is different usage examples:
$(document).ready(function () {
var urlHash = window.location.hash;
var sampleURL = '#categoryId=5&manufacturerId=8';
if ( urlHash.length > 1 ) {
//do stuff
}else{
//if value is empty, do stuff
}
if ( urlHash === sampleURL ) {
commonResponse();
}
$('a').click(function() {
var target = $(this).attr('href');
if (target === sampleURL ) {
commonResponse();
}
});
function commonResponse() {
//alert('ok');
}
});

Categories