Categories
JavaScript Answers

How to clear spy programmatically in Jasmine and JavaScript?

Sometimes, we want to clear spy programmatically in Jasmine and JavaScript.

In this article, we’ll look at how to clear spy programmatically in Jasmine and JavaScript.

How to clear spy programmatically in Jasmine and JavaScript?

To clear spy programmatically in Jasmine and JavaScript, we can just change the spy.

For instance, we write

let spyObj;
beforeEach(() => {
  spyObj = spyOn(obj, "methodName").and.callFake((params) => {});
});

it("should do the declared spy behavior", () => {
  // ...
});

it("should do what it used to do", () => {
  spyObj.and.callThrough();
});

it("should do something differently", () => {
  spyObj.and.returnValue("new value");
});

to create the spyObj object with spyOn.

Then we write spyObj.and.callThrough(); to call obj.methodName in the first test.

In the 2nd test, we call spyObj.and.returnValue to return a new mocked value for obj.methodName.

The existing spy will be cleared after each test.

Conclusion

To clear spy programmatically in Jasmine and JavaScript, we can just change the spy.

Categories
JavaScript Answers

How to check if element is a div with JavaScript?

Sometimes, we want to check if element is a div with JavaScript.

In this article, we’ll look at how to check if element is a div with JavaScript.

How to check if element is a div with JavaScript?

To check if element is a div with JavaScript, we can check the tagName property of the element.

For instance, we write

const myElement = document.getElementById("myElementId");

if (myElement.tagName === "DIV") {
  alert("is a div");
} else {
  alert("is not a div");
}

to select the element with getElementById.

Then we check if the myElement.tagName property returns 'DIV'.

If it is, then myElement is a div.

Conclusion

To check if element is a div with JavaScript, we can check the tagName property of the element.

Categories
JavaScript Answers

How to open window in JavaScript with HTML inserted?

Sometimes, we want to open window in JavaScript with HTML inserted.

In this article, we’ll look at how to open window in JavaScript with HTML inserted.

How to open window in JavaScript with HTML inserted?

To open window in JavaScript with HTML inserted, we can set the innerHTML property of the window’s body element.

For instance, we write

const win = window.open(
  "",
  "Title",
  "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=" +
    (screen.height - 400) +
    ",left=" +
    (screen.width - 840)
);
win.document.body.innerHTML = "HTML";

to call window.open with a string with the dimensions and other settings of the window.

Then we set win.document.body.innerHTML to the HTML string we want to render in the window.

Conclusion

To open window in JavaScript with HTML inserted, we can set the innerHTML property of the window’s body element.

Categories
JavaScript Answers

How to return HTML with fetch() and JavaScript?

Sometimes, we want to return HTML with fetch() and JavaScript.

In this article, we’ll look at how to return HTML with fetch() and JavaScript.

How to return HTML with fetch() and JavaScript?

To return HTML with fetch() and JavaScript, we can use the response text method.

For instance, we write

const fetchMyDocument = async () => {
  try {
    const response = await fetch("/path/to/file.html");
    document.body.innerHTML = await response.text();
  } catch (err) {
    console.log(err);
  }
};

to call fetch to make a GET request to "/path/to/file.html".

Then we get the response from the promise returned by fetch.

Finally, we get the response body HTML from the promise returned by the response.text method with await.

Conclusion

To return HTML with fetch() and JavaScript, we can use the response text method.

Categories
JavaScript Answers

How to copy to clipboard using JavaScript in iOS?

Sometimes, we want to copy to clipboard using JavaScript in iOS.

In this article, we’ll look at how to copy to clipboard using JavaScript in iOS.

How to copy to clipboard using JavaScript in iOS?

To copy to clipboard using JavaScript in iOS, we use the input.setSelectionRange method.

For instance, we write

const copyText = (input) => {
  const isIOSDevice = navigator.userAgent.match(/ipad|iphone/i);

  if (isIOSDevice) {
    input.setSelectionRange(0, input.value.length);
  } else {
    input.select();
  }

  document.execCommand("copy");
};

to define the copyText function.

In it, we check if the function is run on an iOS device by checking the user agent with navigator.userAgent.

We check if 'iPad' or 'iPhone' is in the user agent string.

If it is, we call input.setSelectionRange to select the input’s text.

Otherwise, we use input.select to do the same thing.

Then we call document.execCommand with 'copy' to copy the selected text to the clipboard.

Conclusion

To copy to clipboard using JavaScript in iOS, we use the input.setSelectionRange method.