Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Move to the edit webpage of a webpage via the keyboard with vanilla JavaScript

You're getting close, just need a few adjustments. Instead of using the hostname property, I prefer to use host, because it also includes the port (in case the URL has one) - check the documentati...

posted 2y ago by hkotsubo‭  ·  edited 2y ago by hkotsubo‭

Answer
#4: Post edited by user avatar hkotsubo‭ · 2022-04-20T21:15:00Z (almost 2 years ago)
  • You're getting close, just need a few adjustments.
  • Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.
  • You should also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:
  • ```javascript
  • let domain = window.location.host;
  • window.location.href = domain + '?abc=1';
  • ```
  • The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining (before the `domain`), I get the correct URL (`https://software.codidact.com/?abc=1`).
  • To set the URL you should use `=`, not `===` (the difference is explained [here](https://software.codidact.com/posts/282893)).
  • Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.
  • And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.
  • The final code:
  • ```javascript
  • let domain = window.location.host;
  • let protocol = window.location.protocol;
  • let h1 = document.getElementsByTagName("h1");
  • window.addEventListener('keydown', function(event) {
  • if (event.altKey && event.shiftKey && event.key === 'E') {
  • window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
  • }
  • });
  • ```
  • Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. Note that the delimiter is the character <code>&#96;</code> - you're using double quotes, so `"domain?title=h1&action=edit"` doesn't put the value of the `domain` variable in the string (instead, it puts the word "domain", which is not what you want).
  • But you could also use old-style concatenation:
  • ```javascript
  • window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
  • ```
  • You're getting close, just need a few adjustments.
  • Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.
  • You should also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:
  • ```javascript
  • let domain = window.location.host;
  • window.location.href = domain + '?abc=1';
  • ```
  • The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining (before the `domain`), I get the correct URL (`https://software.codidact.com/?abc=1`).
  • To set the URL you should use `=`, not `===` (the difference is explained [here](https://software.codidact.com/posts/282893)).
  • Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.
  • And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.
  • The final code:
  • ```javascript
  • let domain = window.location.host;
  • let protocol = window.location.protocol;
  • let h1 = document.querySelector("h1");
  • window.addEventListener('keydown', function(event) {
  • if (event.altKey && event.shiftKey && event.key === 'E') {
  • window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
  • }
  • });
  • ```
  • Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. Note that the delimiter is the character <code>&#96;</code> - you're using double quotes, so `"domain?title=h1&action=edit"` doesn't put the value of the `domain` variable in the string (instead, it puts the word "domain", which is not what you want).
  • But you could also use old-style concatenation:
  • ```javascript
  • window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
  • ```
#3: Post edited by (deleted user) · 2022-04-20T21:14:35Z (almost 2 years ago)
I already knew the difference between assignment and comparison, I was just wrong.
  • You're getting close, just need a few adjustments.
  • Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.
  • You should also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:
  • ```javascript
  • let domain = window.location.host;
  • window.location.href = domain + '?abc=1';
  • ```
  • The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining (before the `domain`), I get the correct URL (`https://software.codidact.com/?abc=1`).
  • To set the URL you should use `=`, not `===` (read [here](https://software.codidact.com/posts/282893) to know the difference).
  • Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.
  • And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.
  • The final code:
  • ```javascript
  • let domain = window.location.host;
  • let protocol = window.location.protocol;
  • let h1 = document.getElementsByTagName("h1");
  • window.addEventListener('keydown', function(event) {
  • if (event.altKey && event.shiftKey && event.key === 'E') {
  • window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
  • }
  • });
  • ```
  • Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. Note that the delimiter is the character <code>&#96;</code> - you're using double quotes, so `"domain?title=h1&action=edit"` doesn't put the value of the `domain` variable in the string (instead, it puts the word "domain", which is not what you want).
  • But you could also use old-style concatenation:
  • ```javascript
  • window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
  • ```
  • You're getting close, just need a few adjustments.
  • Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.
  • You should also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:
  • ```javascript
  • let domain = window.location.host;
  • window.location.href = domain + '?abc=1';
  • ```
  • The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining (before the `domain`), I get the correct URL (`https://software.codidact.com/?abc=1`).
  • To set the URL you should use `=`, not `===` (the difference is explained [here](https://software.codidact.com/posts/282893)).
  • Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.
  • And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.
  • The final code:
  • ```javascript
  • let domain = window.location.host;
  • let protocol = window.location.protocol;
  • let h1 = document.getElementsByTagName("h1");
  • window.addEventListener('keydown', function(event) {
  • if (event.altKey && event.shiftKey && event.key === 'E') {
  • window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
  • }
  • });
  • ```
  • Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. Note that the delimiter is the character <code>&#96;</code> - you're using double quotes, so `"domain?title=h1&action=edit"` doesn't put the value of the `domain` variable in the string (instead, it puts the word "domain", which is not what you want).
  • But you could also use old-style concatenation:
  • ```javascript
  • window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
  • ```
#2: Post edited by user avatar hkotsubo‭ · 2022-04-20T11:48:46Z (almost 2 years ago)
  • You're getting close, just need a few adjustments.
  • Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.
  • I also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:
  • ```javascript
  • let domain = window.location.host;
  • window.location.href = domain + '?abc=1';
  • ```
  • The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining, I get the correct URL (`https://software.codidact.com/?abc=1`).
  • To set the URL you should use `=`, not `===` (read [here](https://software.codidact.com/posts/282893) to know the difference).
  • Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.
  • And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.
  • The final code:
  • ```javascript
  • let domain = window.location.host;
  • let protocol = window.location.protocol;
  • let h1 = document.getElementsByTagName("h1");
  • window.addEventListener('keydown', function(event) {
  • if (event.altKey && event.shiftKey && event.key === 'E') {
  • window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
  • }
  • });
  • ```
  • Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. But you could also use old-style concatenation:
  • ```javascript
  • window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
  • ```
  • You're getting close, just need a few adjustments.
  • Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.
  • You should also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:
  • ```javascript
  • let domain = window.location.host;
  • window.location.href = domain + '?abc=1';
  • ```
  • The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining (before the `domain`), I get the correct URL (`https://software.codidact.com/?abc=1`).
  • To set the URL you should use `=`, not `===` (read [here](https://software.codidact.com/posts/282893) to know the difference).
  • Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.
  • And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.
  • The final code:
  • ```javascript
  • let domain = window.location.host;
  • let protocol = window.location.protocol;
  • let h1 = document.getElementsByTagName("h1");
  • window.addEventListener('keydown', function(event) {
  • if (event.altKey && event.shiftKey && event.key === 'E') {
  • window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
  • }
  • });
  • ```
  • Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. Note that the delimiter is the character <code>&#96;</code> - you're using double quotes, so `"domain?title=h1&action=edit"` doesn't put the value of the `domain` variable in the string (instead, it puts the word "domain", which is not what you want).
  • But you could also use old-style concatenation:
  • ```javascript
  • window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
  • ```
#1: Initial revision by user avatar hkotsubo‭ · 2022-04-20T11:42:56Z (almost 2 years ago)
You're getting close, just need a few adjustments.

Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location/host) for more info.

I also get the `protocol` (which can be `http`, `https`, etc), to build the full URL. Without it, you might not get what you want: suppose the current page is `https://software.codidact.com/posts/286256`. If I run this code:

```javascript
let domain = window.location.host;
window.location.href = domain + '?abc=1';
```
The result will be the URL `https://software.codidact.com/posts/software.codidact.com?abc=1`. Only by including the protocol at the beggining, I get the correct URL (`https://software.codidact.com/?abc=1`).

To set the URL you should use `=`, not `===` (read [here](https://software.codidact.com/posts/282893) to know the difference).

Not sure how you're using the `h1` element, I guess you wanted to get its text? I included an example below, that gets the `innerText` property, which contains whatever text is inside the `h1` tag.

And finally, when the `shiftKey` property is set, the corresponding key will be uppercased, so instead of checking for `e`, you actually should check for `E`.

The final code:

```javascript
let domain = window.location.host;
let protocol = window.location.protocol;
let h1 = document.getElementsByTagName("h1");
window.addEventListener('keydown', function(event) {
    if (event.altKey && event.shiftKey && event.key === 'E') {
        window.location.href = `${protocol}//${domain}?title=${h1.innerText}&action=edit`;
    }
});
```

Note that I used [template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to put the variables values inside a single string. But you could also use old-style concatenation:

```javascript
window.location.href = protocol + '//' + domain + '?title='+ h1.innerText + '&action=edit';
```