Getting Current Url

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



Leave a reply



Submit