Tuesday, August 16, 2011

[Javascript] loading URL into a DIV

Sometimes, we want to load in other website into my own site as part of element. Javascript can help perfectly. First of all, we have to place a container for the website you want to display. In this example, I also build a combo box to change different URL for loading. So, the HTML coding should look like,

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Loading URL into a DIV</title>
   
    <style type="text/css">
   
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
   
    #web-panel {
        height: 100%;
        float: right;
        width: 100%;
        overflow: hidden;
    }
   
    #control {
        background: #fff;
        padding: 5px;
        font-size: 14px;
        font-family: Arial;
        border: 1px solid #ccc;
        box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
    }

    </style>

  </head>
  <body>
    <div id="control">
      <strong>URL:</strong>
      <select id="url">
          <option value="">Please select</option>
        <option value="http://www.apple.com">Apple</option>
        <option value="http://www.yahoo.com">Yahoo</option>
        <option value="http://www.google.com">Google</option>
      </select>

    </div>
    <div id="web-panel"></div>
  </body>
</html>

OK, now build the Javascript function to load in the URL. Just simple Javascript is good enough and we don't need jQuery help.
<script type="text/javascript">
    function loadURL(u) {
        document.getElementById("web-panel").innerHTML = '<iframe src="' + u + '" width="100%" height="100%" border="0"></iframe>';
    }
</script>

This function should be triggered by combo bo onChange function. So, here is the complete code.
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Loading URL into a DIV</title>
   
    <style type="text/css">
   
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
   
    #web-panel {
        height: 100%;
        float: right;
        width: 100%;
        overflow: hidden;
    }
   
    #control {
        background: #fff;
        padding: 5px;
        font-size: 14px;
        font-family: Arial;
        border: 1px solid #ccc;
        box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
    }

    </style>

  <script type="text/javascript">
    function loadURL(u) {
        document.getElementById("web-panel").innerHTML = '<iframe src="' + u + '" width="100%" height="100%" border="0"></iframe>';
    }
  </script>
  </head>
  <body>
    <div id="control">
      <strong>URL:</strong>
      <select id="url" onchange="loadURL(this.value);">
          <option value="">Please select</option>
        <option value="http://www.apple.com">Apple</option>
        <option value="http://www.yahoo.com">Yahoo</option>
        <option value="http://www.google.com">Google</option>
      </select>

    </div>
    <div id="web-panel"></div>
  </body>
</html>
Related Posts Plugin for WordPress, Blogger...