Get the current URL with JavaScript?
Use:
window.location.href
As noted in the comments, the line below works, but it is bugged for Firefox.
document.URL
See URL of type DOMString, readonly.
How to get current URL with JavaScript?
If you are currently on http://store.ijdmtoy.com/abs.htm then document.URL
will be http://store.ijdmtoy.com/abs.htm
.
You can try:
url = location.protocol + '//' + location.host + location.pathname + '?Search=' + str;
Get current URL address by javascript
have you tried:
var url = window.location.pathname;
Get current URL
You can get the URL like this
var url = require('sdk/tabs').activeTab.url;
This works only inside your add-on code, not inside content scripts. For the difference see this guide.
If you want to trigger the lookup from the content script and/or want to use the URL in the content script, you will have to communicate using "port".
Here is an example in which the url of the current tab is displayed in the panel upon clicking a button.
lib/main.js (add-on code):
const data = require('sdk/self').data;
var panel = require('sdk/panel').Panel({
contentURL: data.url('panel.html'),
contentScriptFile: data.url('panel.js')
});
panel.port.on('get url', function () {
panel.port.emit('return url', require('sdk/tabs').activeTab.url);
});
panel.show();
data/panel.js (content script):
var button = document.getElementById('button');
var urlspan = document.getElementById('url');
button.addEventListener('click', function () {
self.port.emit('get url');
});
self.port.on('return url', function (url) {
urlspan.textContent = url;
});
data/panel.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>panel</title>
</head>
<body>
<button id="button">
Get Current URL
</button>
<br>
Current URL: <span id="url"></span>
</body>
</html>
Get current url in Angular
With pure JavaScript:
console.log(window.location.href)
Using Angular:
this.router.url
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
template: 'The href is: {{href}}'
/*
Other component settings
*/
})
export class Component {
public href: string = "";
constructor(private router: Router) {}
ngOnInit() {
this.href = this.router.url;
console.log(this.router.url);
}
}
The plunkr is here: https://plnkr.co/edit/0x3pCOKwFjAGRxC4hZMy?p=preview
Related Topics
How to Get First X Chars from a String, Without Cutting Off the Last Word
Regexp in Preg_Match Function Returning Browser Error
PHP Preg_Match to Find Whole Words
Why and How to Use Anonymous Functions in PHP
Return the Portion of a String Before the First Occurrence of a Character in PHP
Best Way to Process Large Xml in PHP
Failed to Open Stream: Http Wrapper Does Not Support Writeable Connections
How to Set, Get and Destroy Cookies in Wordpress
Ssl Error Ssl3_Get_Server_Certificate:Certificate Verify Failed
"Strict Standards: Only Variables Should Be Passed by Reference" Error
A Yaml File Cannot Contain Tabs as Indentation
Fastest Way to Add Prefix to Array Keys