2012-06-01 5 views
8

क्या ब्राउज़र विंडो को अग्रभूमि/जावा एप्लेट अलर्ट विंडो से फोकस करने का कोई तरीका है? मेरे पास एक HTML पृष्ठ में एक एप्लेट है जो इसमें एक बटन के साथ अलर्ट लाता है। जब बटन दबाया जाता है, तो मैं चाहता हूं कि मूल ब्राउज़र विंडो जहां भी हो (पॉपमाइज्ड, कवर, इत्यादि) से पॉप अप करें। मुझे विश्वास है कि ऐसा करने के लिए जावा को जावास्क्रिप्ट से कनेक्ट करने का एक तरीका है, लेकिन मुझे जावास्क्रिप्ट नहीं पता है। यहाँजावा/जावास्क्रिप्ट ब्राउज़र विंडो को अग्रभूमि में एप्लेट युक्त लाता है

/** An applet that posts an alert and waits for the alert button to be pressed. 
* Version 1 uses http://java.sun.com/products/plugin/1.3/docs/jsobject.html 
*/ 

import netscape.javascript.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Bounce extends JApplet implements ActionListener { 
    JDialog dialog; 
    JSObject window; 
    String message; 

    public void paint(Graphics g) { 
     g.clearRect(0,0, 400,40); 
     g.drawString(message,40,20); 
    } 

    public void init() { 
     JFrame frame= null; 
     dialog= new JDialog(frame, "Bounce App"); 

     JButton setupButton= new JButton("Bounce it back!"); 
     setupButton.addActionListener(this); 

     JPanel contentPane= new JPanel(); 
     contentPane.add(setupButton); 
     contentPane.setOpaque(true); 
     dialog.setContentPane(contentPane); 

     dialog.setSize(new Dimension(400, 110)); 
     dialog.setVisible(true); 

     message= "This applet posts an alert panel."; 
     window= JSObject.getWindow(this); 
//  String[] params= { "An alert message" }; 
//  window.call("alert", params); 
//  window.eval("alert('Important Alert!')"); 
    } 

    public void actionPerformed(ActionEvent e) { 
     dialog.setVisible(false); 
     dialog.dispose(); 
     System.err.println("button has been pushed; focus set"); 
     message= "Somebody pushed my bounce-back button."; 
     JSObject document= (JSObject)window.getMember("document"); 
     document.setMember("bgColor", "orange"); 
     window.eval("focus()"); 
     repaint(); 
    } 

} 

और एचटीएमएल कोड है:

यहाँ जावा एप्लेट कोड है

<HTML> 
    <HEAD><TITLE>The Reappearing Page</TITLE></HEAD> 
    <body bgcolor="#f0ffc0"> 

    <H2>Make this page reappear</H2> 
    This page will start an applet (white box below) that sets up an alert. 
    Before you respond to the alert, hide the window you are reading right now, 
    using one of these methods:<ul> 
    <li> cover it with another window, </li> 
    <li> Hide it using a menu item, </li> 
    <li> Minimize it, or </li> 
    <li> move it to another workspace or desktop. </li> 
    </ul> 
    Then click on the button in the alert. 

    <P> 
    <EMBED type="application/x-java-applet;version=1.3" width="400" height="40" 
     align="baseline" code="Bounce.class" MAYSCRIPT=true 
     pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"> 
    <NOEMBED> 
     No JDK 1.3 support for APPLET!! 
    </NOEMBED> 
    </EMBED> 

    <P>What is supposed to happen is that the main window 
    will emerge from wherever you hid it and reappear. 
    Since I don't know how to do this, it is your challenge to actually make it happen. 
    We need to be able to achieve this magic from any of the situations listed above.</P> 

    </body> 

उत्तर

1

(टिप्पणी से बदल दिया है मैं एक सभ्य कोड स्निपेट पोस्ट कर सकते हैं तो जवाब देने के लिए आपके लिए) जावास्क्रिप्ट का उपयोग करते हुए एक विंडो "फोरग्राउंड" करने का कोई भरोसेमंद तरीका नहीं है, जब तक कि यह एक विंडो नहीं है जिसे आपने अपने आप को मूल पृष्ठ से बनाया है, स्पष्ट कारणों से ("बधाई हो !! आप हमारे 1000 वें सीयू हैं Stomer, आप एक पुरस्कार जीत !!!!! ")। आप एक पॉपअप एक और खिड़की से window.open का उपयोग के रूप में अपने एप्लेट खिड़की लोड किया है, तो आप window.focus का उपयोग कर की कोशिश कर सकते:

var w = window.open(appletPageUrl); 
w.focus() 

मैं कल्पना पॉपअप ब्लॉकर्स बात की इस तरह की सराहना करते नहीं है, इसलिए YMMV। एक व्यक्तिगत नोट पर, मैं सुझाव दूंगा कि आपके बारे में कोई विचार है कि कोई उपयोगकर्ता आपकी अलर्ट के बारे में जानना चाहता है कि क्या उन्होंने आपके ऐप को छुपा/छोटा करना चुना है।

संबंधित मुद्दे