Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Browser.setText + links
Browser.setText + links [message #463903] Tue, 15 November 2005 16:03 Go to next message
James Willans is currently offline James WillansFriend
Messages: 303
Registered: July 2009
Senior Member
Hi,

We dynamically create a HTML page and display it using
Browser.setText(..). These pages have no file representation and often
contain links (to other parts of the same page) of the form:

<A HREF="#link">This is a link<A/>

<A NAME="link"/>

The problem we're having is that the links are displayed but don't work.
How do we go about using links for in-memory only HTML pages?

Thanks,

James
Re: Browser.setText + links [message #463991 is a reply to message #463903] Wed, 16 November 2005 10:24 Go to previous messageGo to next message
James Willans is currently offline James WillansFriend
Messages: 303
Registered: July 2009
Senior Member
In case it isn't clear, I've modified one of the swt snippets (below) to
demonstrate what I am trying to achieve. Basically when I clock on the
link "This is a link" I get blank#link displayed in the browser, instead I
would like the browser it to jump to the link #link:

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

public static void main(String[] args) {
/* Relative links: use the HTML base tag */
String html = "<html><head>"
+ "<title>HTML Test</title></head>"
+ "<body><A HREF=\"#link\">This is a link<A/><br/><br/>"
+ "<A NAME=\"link\">This is the target<A/></body></html>";

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: Browser.setText + links [message #464002 is a reply to message #463991] Wed, 16 November 2005 11:54 Go to previous messageGo to next message
Benjamin Pasero is currently offline Benjamin PaseroFriend
Messages: 337
Registered: July 2009
Senior Member
Add this into the HEAD of your HTML:

<base href="<Absolute URL Here>">

All relative Links will then be resolved using the BASE URL.

Ben


> In case it isn't clear, I've modified one of the swt snippets (below) to
> demonstrate what I am trying to achieve. Basically when I clock on the
> link "This is a link" I get blank#link displayed in the browser, instead
> I would like the browser it to jump to the link #link:
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.browser.Browser;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> public class Test {
>
> public static void main(String[] args) {
> /* Relative links: use the HTML base tag */
> String html = "<html><head>"
> + "<title>HTML Test</title></head>"
> + "<body><A HREF=\"#link\">This is a link<A/><br/><br/>"
> + "<A NAME=\"link\">This is the target<A/></body></html>";
>
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> Browser browser = new Browser(shell, SWT.NONE);
> browser.setText(html);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
>
Re: Browser.setText + links [message #464004 is a reply to message #464002] Wed, 16 November 2005 12:07 Go to previous messageGo to next message
James Willans is currently offline James WillansFriend
Messages: 303
Registered: July 2009
Senior Member
Thanks Benjamin, but I'm not sure that's what I'm asking. The link I am
describing is to a different part of the same in-memory document that
contains the link, I don't want to navigate to a new document resolved by
the value of 'base'.

James

Benjamin Pasero wrote:

> Add this into the HEAD of your HTML:

> <base href="<Absolute URL Here>">

> All relative Links will then be resolved using the BASE URL.

> Ben


>> In case it isn't clear, I've modified one of the swt snippets (below) to
>> demonstrate what I am trying to achieve. Basically when I clock on the
>> link "This is a link" I get blank#link displayed in the browser, instead
>> I would like the browser it to jump to the link #link:
>>
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.browser.Browser;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Shell;
>>
>> public class Test {
>>
>> public static void main(String[] args) {
>> /* Relative links: use the HTML base tag */
>> String html = "<html><head>"
>> + "<title>HTML Test</title></head>"
>> + "<body><A HREF="#link">This is a link<A/><br/><br/>"
>> + "<A NAME="link">This is the target<A/></body></html>";
>>
>> Display display = new Display();
>> Shell shell = new Shell(display);
>> shell.setLayout(new FillLayout());
>> Browser browser = new Browser(shell, SWT.NONE);
>> browser.setText(html);
>> shell.open();
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch())
>> display.sleep();
>> }
>> display.dispose();
>> }
>> }
>>
Re: Browser.setText + links [message #464010 is a reply to message #463991] Wed, 16 November 2005 14:27 Go to previous messageGo to next message
Steven Wasleski is currently offline Steven WasleskiFriend
Messages: 23
Registered: July 2009
Junior Member
James Willans wrote:
> In case it isn't clear, I've modified one of the swt snippets (below) to
> demonstrate what I am trying to achieve. Basically when I clock on the
> link "This is a link" I get blank#link displayed in the browser, instead
> I would like the browser it to jump to the link #link:
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.browser.Browser;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> public class Test {
>
> public static void main(String[] args) {
> /* Relative links: use the HTML base tag */
> String html = "<html><head>"
> + "<title>HTML Test</title></head>"
> + "<body><A HREF=\"#link\">This is a link<A/><br/><br/>"
> + "<A NAME=\"link\">This is the target<A/></body></html>";
>
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> Browser browser = new Browser(shell, SWT.NONE);
> browser.setText(html);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
>
James,

I am not sure if this could be your problem, but your <A/> end tags
should be </A>. Most browsers are pretty forgiving on this but perhaps
the one you are using is not.

Also, with just two line breaks between the link and the target, how can
you be sure it is not working. If the target is visible on the page,
most browsers will not scroll. You may want to put in a bunch more <BR>
tags for testing.

Steve
Re: Browser.setText + links [message #464167 is a reply to message #464010] Thu, 17 November 2005 17:54 Go to previous messageGo to next message
James Willans is currently offline James WillansFriend
Messages: 303
Registered: July 2009
Senior Member
Hi Steve,

Thanks for your suggestion, but unfortunately this is not the problem. I
know that it is not working because when I click on the link, I get a new
page displaying only "blank#link". I'm beginning to think that there is
no easy solution to this.

James

> James,

> I am not sure if this could be your problem, but your <A/> end tags
> should be </A>. Most browsers are pretty forgiving on this but perhaps
> the one you are using is not.

> Also, with just two line breaks between the link and the target, how can
> you be sure it is not working. If the target is visible on the page,
> most browsers will not scroll. You may want to put in a bunch more <BR>
> tags for testing.

> Steve
Re: Browser.setText + links [message #464169 is a reply to message #464167] Thu, 17 November 2005 21:13 Go to previous messageGo to next message
Steven Wasleski is currently offline Steven WasleskiFriend
Messages: 23
Registered: July 2009
Junior Member
James Willans wrote:
> Hi Steve,
>
> Thanks for your suggestion, but unfortunately this is not the problem.
> I know that it is not working because when I click on the link, I get a
> new page displaying only "blank#link". I'm beginning to think that
> there is no easy solution to this.
>
> James
>
James,

I attached a LocationListener to the Browser object in your example and
watched what was going by. On initial load of your page, the location
is "about:blank" and upon clicking the link, it is "about:blank#link".
It appears there is a special "about:blank" page ("about" protocol to an
otherwise "blank" page) that is used to display contents set via the
setText method so that your base URL is set to "about:blank", therefore,
relative links like "#link" resolve to "about:blank#link". The resolved
URL is then treated like it came from setText("blank#link"), that is the
protocol is stripped and the rest is considered to be the page contents
and so you see what you see. I would recommend you open a bug report to
see if this could be handled better.

In the mean time, here is what I was able to get pretty much working as
you want it to. I had to add an onClick handler to do the scroll myself
and not let the browser handle it. Sorry about any formatting problems.
Also note that the '\n' between the two statements in the onClick
handler is very important.

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

public static void main(String[] args) {
/* Relative links: use the HTML base tag */
final String html = "<html><head>"
+ "<title>HTML Test</title></head>"
+ "<body><A ID=\"foo\" HREF=\"#link\">This is a link</A>"
+ "\n<script for=\"foo\" event=onclick
language=\"javascript\">document.all.link.scrollIntoView()\nreturn
false</script>"
+
"\n<br><br><br><br><br><br><br><br><br><br><br><br ><br><br><br><br><br><br><br><br><br><br><br><br> <br><br><br><br><br><br><br>"
+ "<A NAME=\"link\">This is the target</A></body></html>";

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: Browser.setText + links [message #464205 is a reply to message #464169] Fri, 18 November 2005 17:35 Go to previous message
James Willans is currently offline James WillansFriend
Messages: 303
Registered: July 2009
Senior Member
Hi Steve,

> I attached a LocationListener to the Browser object in your example and
> watched what was going by. On initial load of your page, the location
> is "about:blank" and upon clicking the link, it is "about:blank#link".
> It appears there is a special "about:blank" page ("about" protocol to an
> otherwise "blank" page) that is used to display contents set via the
> setText method so that your base URL is set to "about:blank", therefore,
> relative links like "#link" resolve to "about:blank#link". The resolved
> URL is then treated like it came from setText("blank#link"), that is the
> protocol is stripped and the rest is considered to be the page contents
> and so you see what you see. I would recommend you open a bug report to
> see if this could be handled better.

> In the mean time, here is what I was able to get pretty much working as
> you want it to. I had to add an onClick handler to do the scroll myself
> and not let the browser handle it. Sorry about any formatting problems.
> Also note that the 'n' between the two statements in the onClick
> handler is very important.

Many thanks for your help, it is really appreciated. I have posted bug
117108 to deal with this issue, in the meantime I'll give your suggestion
a try.

Thanks again,

James

> import org.eclipse.swt.SWT;
> import org.eclipse.swt.browser.Browser;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;

> public class Test {

> public static void main(String[] args) {
> /* Relative links: use the HTML base tag */
> final String html = "<html><head>"
> + "<title>HTML Test</title></head>"
> + "<body><A ID="foo" HREF="#link">This is a link</A>"
> + "n<script for="foo" event=onclick
> language="javascript">document.all.link.scrollIntoView()nreturn
> false</script>"
> +
>
"n<br><br><br><br><br><br><br><br><br><br><br><br ><br><br><br><br><br><br><br><br><br><br><br><br> <br><br><br><br><br><br><br>"
> + "<A NAME="link">This is the target</A></body></html>";

> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> Browser browser = new Browser(shell, SWT.NONE);
> browser.setText(html);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
Previous Topic:SWT/AWT flickering problem
Next Topic:Inline images in text controls
Goto Forum:
  


Current Time: Fri Mar 29 14:38:00 GMT 2024

Powered by FUDForum. Page generated in 0.07133 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top