Javascript Browser Window frameElement Property

Introduction

Find out if the current window is in an <iframe>. If so, change its URL to "java2s.com":

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from  ww w  .j av  a  2  s .c  o  m
  var frame = window.frameElement;

  if (frame) {
    frame.src = "https://www.java2s.com/";
  }
}
</script>

</body>
</html>

The frameElement property returns the <iframe> element in which the current window is inserted.

If the document window is not placed within an <iframe>, the return value of this property is null.

This property is read-only.




PreviousNext

Related