Running JQuery script in SWT browser

依然范特西╮ 提交于 2019-12-11 07:47:16

问题


I am developing an Java application with an embedded SWT browser in it.I want to run a jquery script on the SWT browser.I have placed by jquery library file in the src folder of the project.And here is the program.

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.AuthenticationEvent;
import org.eclipse.swt.browser.AuthenticationListener;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;


public class Dummy3 {

    static int x=1;
    public static void main(String args[])
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        //static int x=1;
        final Browser browser = new Browser(shell, SWT.NONE);


        final String s= "<html>"+
                        "<head>"+
                        "<title>The jQuery Example</title>"+
                        "<script type=\"text/javascript\""+ 
                        "src=\"jquery-2.1.1.min.js\"></script>"+

                        "<script type=\"text/javascript\" language=\"javascript\">"+
           // <![CDATA[
                        "$(document).ready(function() {"+
                        "$(\"div\").click(function() {"+
                        "alert(\"Hello world!\");"+
                        "});"+
                        "});"+
           // ]]>
                        "</script>"+

                        "</head>"+
                        "<body>"+
                        "<div id=\"newdiv\">"+
                        "Click on this to see a dialogue box."+
                        "</div>"+
                        "</body>"+
                        "</html>" ;

        browser.setText(s);


        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }



    }

}

回答1:


The problem is because the script is not able to locate lib>there is one solution here Show local HTML file with JavaScript in SWT Browser widget in RAP .

I have just solved this by changing src to

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Taken this link from W3Schools.com "Try Editor"



来源:https://stackoverflow.com/questions/26417811/running-jquery-script-in-swt-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!