13

So, I am using IBM Worklight where I have the main file called file1.html and then I created another html file called file2.html.

I am trying to open file2 but no luck so far. I tried following pieces of code:

  1. $(this).load("file2.html");

  2. $("div1").load("file2.html"); //div1 is the id for outer div of file1

  3. WL.App.openUrl("file2.html");

  4. window.openURL("file2.html");

And none of these worked! Any suggestions?

Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • tried `window.location.href = 'file2.html'` ? – Hamza Waqas Oct 19 '12 at 23:44
  • 1
    If you're selecting by `id` then you need to preface the `id` with a `#`: `$('#div1').load(/* ...other stuff... */);` – David Thomas Oct 20 '12 at 00:02
  • 1
    Open how? In a new separate browser tab? Or in the same tab (replacing the current page)? Or as a pop-up window? Or into an IFRAME element in the current page? Or just dump the contents of the second page into the current page? You have to be more specific. – Šime Vidas Oct 20 '12 at 00:18

4 Answers4

48

use window.open("file2.html"); to open on new window,

or use window.location.href = "file2.html" to open on same window.

Habibillah
  • 27,347
  • 5
  • 36
  • 56
7

Use window.open("file2.html");

Syntax

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

Return value and parameters

windowObjectReference 

A reference to the newly created window. If the call failed, it will be null. The reference can be used to access properties and methods of the new window provided it complies with Same origin policy security requirements.

strUrl 

The URL to be loaded in the newly opened window. strUrl can be an HTML document on the web, image file or any resource supported by the browser.

strWindowName 

A string name for the new window. The name can be used as the target of links and forms using the target attribute of an <a> or <form> element. The name should not contain any blank space. Note that strWindowName does not specify the title of the new window.

strWindowFeatures 

Optional parameter listing the features (size, position, scrollbars, etc.) of the new window. The string must not contain any blank space, each feature name and value must be separated by a comma.

btiernay
  • 7,873
  • 5
  • 42
  • 48
5

If you want to use jQuery, the .load() function is the correct function you are after;

But you are missing the # from the div1 id selector in the example 2)

This should work:

$("#div1").load("file2.html");
Hank
  • 731
  • 6
  • 10
  • okay this works just fine $.get("file2.html", function (deals) { $('#wrapper').html(deals); getDeals(deals);...but now how can i get access to deals in file2..file2 has different jQuery file to it?? Plzz help }); – Cute_Ninja Oct 21 '12 at 22:51
  • or do `$("body").load("index.html")` to render a new page – prayagupa Jul 14 '18 at 03:33
  • This opens the page inside the modal (if you are inside a modal window). – user2060451 Sep 29 '19 at 18:56
1

You need to use ajax.

http://api.jquery.com/jQuery.ajax/

<code>
$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});
</code>
  • Or if you want to redirect the user to the other page use this window.location.href = "file2.html"; –  Oct 19 '12 at 23:47