Categories
JavaScript Answers

How to get a button element to link to a location without an anchor element with JavaScript?

Sometimes, we want to get a button element to link to a location without an anchor element with JavaScript.

In this article, we’ll look at how to get a button element to link to a location without an anchor element with JavaScript.

How to get a button element to link to a location without an anchor element with JavaScript?

To get a button element to link to a location without an anchor element with JavaScript, we set the location on click.

For instance, we write

const gotoUrl = () => {
  window.location.assign("https://www.example.com/");
};

document.getElementById("button").addEventListener("click", gotoUrl);

to get the button with getElementById.

Then we call addEventListener to add the gotoUrl click listener to it.

In gotoUrl, we call window.location.assign to go to the URL on click.

Conclusion

To get a button element to link to a location without an anchor element with JavaScript, we set the location on click.

Categories
JavaScript Answers

How to write elements into a child iframe using JavaScript?

Sometimes, we want to write elements into a child iframe using JavaScript.

In this article, we’ll look at how to write elements into a child iframe using JavaScript.

How to write elements into a child iframe using JavaScript?

To write elements into a child iframe using JavaScript, we use the contentDocument property.

For instance, we write

const iframeDocument = document.getElementById("iframeID").contentDocument;

to get the iframe with getElementById.

Then we get its document object with contentDocument.

Conclusion

To write elements into a child iframe using JavaScript, we use the contentDocument property.

Categories
JavaScript Answers

How to sort a string alphabetically using a function with JavaScript?

Sometimes, we want to sort a string alphabetically using a function with JavaScript.

In this article, we’ll look at how to sort a string alphabetically using a function with JavaScript.

How to sort a string alphabetically using a function with JavaScript?

To sort a string alphabetically using a function with JavaScript, we call the split, sort, and join methods.

For instance, we write

const sortAlphabets = (text) => {
  return text.split("").sort().join("");
};

to call split to split the string into an array of character strings.

Then we call sort to sort the array.

And then we call join to combine the sorted characters back to a string.

Conclusion

To sort a string alphabetically using a function with JavaScript, we call the split, sort, and join methods.

Categories
JavaScript Answers

How to wait till element is displayed with JavaScript Selenium WebDriver?

Sometimes, we want to wait till element is displayed with JavaScript Selenium WebDriver.

In this article, we’ll look at how to wait till element is displayed with JavaScript Selenium WebDriver.

How to wait till element is displayed with JavaScript Selenium WebDriver?

To wait till element is displayed with JavaScript Selenium WebDriver, we use the wait and elementIsVisible methods.

For instance, we write

const el = await driver.findElement(By.id(`import-file-acqId:${acqId}`));
await driver.wait(until.elementIsVisible(el), 100);
await el.sendKeys(file);

to call findElement to find the element by its iD.

Then we call driver.wait to wait for the element until it’s visible and time out in 100ms.

Then we call sendKeys to press the key on it.

Conclusion

To wait till element is displayed with JavaScript Selenium WebDriver, we use the wait and elementIsVisible methods.

Categories
JavaScript Answers

How to check if parent window is iframe or not with JavaScript?

Sometimes, we want to check if parent window is iframe or not with JavaScript.

In this article, we’ll look at how to check if parent window is iframe or not with JavaScript.

How to check if parent window is iframe or not with JavaScript?

To check if parent window is iframe or not with JavaScript, we use ther window.frameElement property.

For instance, we write

const isInIframe =
  window.frameElement && window.frameElement.nodeName === "IFRAME";

to check if the nodeName property of the window.frameElement object if 'IFRAME'.

If it is, then the parent window is an iframe.

Conclusion

To check if parent window is iframe or not with JavaScript, we use ther window.frameElement property.