Which method to use to access page URLs? - javascript

For WebExtensions, there are several methods to check a page's URL:
window.location.href in a content script,
the webRequest API, and
tabs.onUpdated.
Which one to use when you just need to check the URL, and maybe redirect based on it? Which is easiest to code? Are there reasons for using the others?

For your specific use case use the best method is
window.location.href
if you want to get only domain name instead of full URL, use
window.location.hostname
For complete guide on Window Location API please check https://developer.mozilla.org/en-US/docs/Web/API/Window/location
Note: You mentioned document.location.href which is not supported on some version of Firefox browser

Related

How to use window.open(url,"_blank") without changing calling application's url?

This is my js window.open() code.
var url = "https://www.google.com"
window.open(url, "_blank", "menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes,width=640,height=480", false);
It opens a new browser window with the "url" as expected.
But the issue is that, it also changes the current url in my main web application.
For example before my window.open() call if I was on this url:
https://example.com/#!/projects/56asda/view
After the call the url redirects to this:
https://example.com/#!/
How can this issue be prevented? I have not found any solution for this on the internet.
I am using angularjs 1.0 for my frontend.
Please refrain from answering that "_blank" should work etc.
Any help will be invaluable.
I have tried to duplicate your issue but I have found no issues while executing the code which would mean your issue might be related to your web server settings.
Anyways, have you tried calling it like this:
var url = "https://yourlink.here";
window.open(url, "_blank");
Without anything like "menubar=no" or other arguments?
If you haven't, I suggest you do and if that works, just add the arguments one by one to check which one contains the error.

Set Referrer value on a following called url

I have an Html file containing the following code:
<script>
Object.defineProperty(document, "referrer", {get : function(){ return "myreferrer.com"; }});
//document.location="somelink.com";
</script>
From what I've read,maybe the thing I'm trying cannot be done,but I wanted to be sure.
I want to visit the site somelink.com but when my browser finishes the redirection to the location,the document.referrer value to be "myreferrer.com".
If I run the html with this format(document.location in comments)
the command in url --> javascript:alert(document.referrer) is the one I want.
But if I erase the comments and activate the document.location line,the above command will show up an empty document.referrer and not the one I want.
Can I achieve what I have in mind?
Some browser versions allowed you to customize the referer header using the approach of overriding the document.referer property in javascript, but that doesn't appear to be reliable. Even if some browsers still allow that, there's no guarantee it would work in future versions.
If you need a workaround, you could link to the desired referrer domain and serve up an intermediate page that performs the navigation to the final destination URL via an HTML form submission. That should preserve that intermediate page as the referrer.
Within the context of a browser extension however, you can alter the headers via onBeforeSendHeaders

JavaScript set HTTP request header value and FireFox issue

In order to prevent direct url access to download files on my website I use following HTTP header which is setted by the following JavaScript during the click on download url:
$('a.download').on('click', function(){
$.ajax({
url: '/ajax/preventDownload',
headers: { 'x-rarity-download-header': 'download' }
});
})
Server checks if this 'x-rarity-download-header' present in HTTP request and if no doesn't allow user to download file.
Right now this approach works not in the all browsers, for example it works in FireFox 50 and looks like doesn't work on some previous versions like 48. Also, this approach doesn't work in Safari browser.
What can be a reason of this and how to fix it ?
First, you should respect the spelling. It should be X-Rarity-Download-Header.
Some software do take this more serious than others and only allow standarized headers or custom headers set with uppercase X-.
Besides that, I suggest you to switch to a more common method like oauth2 tokens.
Or something way easier, like:
1. Visit the site
2. Set a cookie
3. Allow download.
4. If cookie is not set, don't allow to download.
You can also try beforeSend method from jQuery, which seems a little better place to add headers: http://api.jquery.com/jquery.ajax/

What is the difference between window.location and $location.path?

In MVC angularJS application, how can I redirect to MVC page.
I tried below two options
First
// It doesn't work
$location.path("/MyPage1");
Second
//It works
window.location = "/MyPage1";
Please suggest best way to redirect and why ?
REMEMBER : I am not using angularJs Routing.
Comparing $location to window.location official doc clearly stated
see the section at this location
seamless integration with HTML5 API
window.location: no
$location: yes (with a fallback for legacy browsers)
and more
Both do have their own merits. They are clearly described in the official docs as mentioned by #Mohammad. So depending on the circumstances choose any of the either :
Use $location : When you do not require a full page reload when the browser URL is changed, wants to avail the angular internal life-cycle benefits and where you don't need to support old legacy browsers.This might be useful if your destination is just a variation on the current URL, so that you can take advantage of $location helper methods. E.g. we ran $location.search(..., ...) to just change value of a querystring paramater.
Use native window location : When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: window.location.href or when you want to work with row level object properties that you can directly modified. i.e like Force reload window.location.reload().

How to know that WINDOW.LOCATION load url

I need to check if iPhone Safari support specific url (lppn://playnow - that url will run Texas Poker application if its already installed) if url does not supported i want to run other link (application page in AppStore).
Are WINDOW.LOCATION return false if it can not load uri?
window.location won't return false, however you can use AJAX for this.
If jQuery is relevant, it's matter of one or two lines of code (AJAX call to the page, handle the onerror event) otherwise you'll have to build it yourself, but it's still possible.

Categories

Resources