window.location.href and window.open () methods in JavaScript
window.location.href
is not a method, it's a property that will tell you the current URL location of the browser. Changing the value of the property will redirect the page.
window.open()
is a method that you can pass a URL to that you want to open in a new window. For example:
window.location.href example:
window.location.href = 'http://www.google.com'; //Will take you to Google.
window.open() example:
window.open('http://www.google.com'); //This will open Google in a new window.
Additional Information:
window.open()
can be passed additional parameters. See: window.open tutorial
Open window.location.href location in a new window
function onDropownChange(e){
var value = document.getElementById('dropDown').value; switch(value){ case 'A': window.open('https://www.google.com','_blank'); case 'B': window.open('https://stackoverflow.com/','_blank'); }
}
<!DOCTYPE html><html><head> <title>Example</title></head><body>
<select name="example" id="dropDown" onchange="onDropownChange()"> <option value="A">Option A</option> <option value="B">Option B</option></select>
<script>
</script>
</body></html>
What's the difference between window.open(url) and window.location.href = url on Firefox?
The difference between window.open()
and window.location.href
is that open()
is a method of the window class, and window.location
is a property of the window class.
1.window.open()
is a method on the window class
Calling the window.open()
method actually creates a window object, which can be held in a variable and manipulated according to your program's requirements.
To demonstrate that window.open() actually returns a window object, consider the following code:
var mywindow = window.open("http://google.com");
mywindow.name = "Awesome Window";
console.log(typeof(mywindow)); // --> "object"
console.log(mywindow.name); // --> "Awesome Window"
The reason your code was opening an unwanted window, is because you were calling window.open()
, whose sole purpose in life is to open a new window.
2. window.location
is a read-only property on the window class.
Although window.location
is a read-only property, the window.location
has a built-in shortcut feature that allows window.location
to be assignable, which has the same effect as calling window.location.assign()
, which does not return a window object, but uses the root window object to assign a new url to, causing the newly-assigned url to be loaded in the browser window where the javascript assigning the location was called.
If you are creating a bookmarket script, then using window.location
is the better way of grabbing the current window's url and assigning it to your program's url string.
The reason why you might find that you are getting unexpected behavior in different browsers, is that there is no official public standard set for the window object, so how each browser chooses to implement it behind the scenes may vary.
window.open vs window.location.href
if window.opener == null
// open by window.location.href
else
// open by window.open
Window.location.href () and Window.open () in JavaScript
window.location
is an Object and
window.location.href
is its property
It tells you the current URL location of the browser
document.write(location.href);// will give location URL location of browser.
Setting the property will redirect the page.
window.open()
is a method that you can pass a URL to that you want to open in a new window
E.g
window.location.href = 'http://www.xyz.com'; //Will take you to xyz.
window.open('http://www.xyz.com'); //This will open xyz in a new window.
JavaScript: location.href to open in new window/tab?
window.open(
'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
'_blank' // <- This is what makes it open in a new window.
);
JavaScript window.location.href and click() methods doesn't work on mobile phone
You must call another tab to use "tel" action, try like this:
$(function() {
$('.phone').click(function() {
var PhoneNumber = $(this).text();
PhoneNumber = PhoneNumber.replace('Phone:', '_self');
//PhoneNumber = PhoneNumber.replace('Phone:', ''); or without _self
window.location.href = 'tel://' + PhoneNumber;
// you can use window.open instead like this: window.open('tel:900300400')
});
});
In HTML:
<span class="phone">Phone: 900 300 400</span>
Related Topics
What Does "This" Mean in Jquery
Group Objects by Multiple Properties in Array Then Sum Up Their Values
Sorting a JavaScript Object by Property Name
Why Does a While Loop Block the Event Loop
How to Gain Access to the Closure of a Function
How to Add a Custom Right-Click Menu to a Webpage
Object Spread VS. Object.Assign
Differencebetween Children and Childnodes in JavaScript
Prevent Scrolling of Parent Element When Inner Element Scroll Position Reaches Top/Bottom
Js Client-Side Exif Orientation: Rotate and Mirror Jpeg Images
Make Axios Send Cookies in Its Requests Automatically
Get the Closest Number Out of an Array
How to Get Progress from Xmlhttprequest
Angularjs "Controller As" Syntax - Clarification
Please Explain the Use of JavaScript Closures in Loops
Trying to Fire the Onload Event on Script Tag