Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following php code on my server with Javascript to open windows. When I execute this code in my browser window, it opens the necessary windows. However, if this program is called by another server application (on it's own trigger), the windows do not open as expected in Javascript - any other logic in the code does get executed. For example, if I add a write mySQL table command in the following code, it writes the table when called by an external program but does not open windows locally.
Ideally, I want the program to start executing (and open windows) when called by the external program.
This is the code
<?php


// ideally this code should be executed when called by an external application as POST TO
//$jsondata=file_get_contents('php://input');

// dummy data used for testing
$json='{"stocks":"AMBUACEM,BALRAMCHIN","trigger_prices":"423.55,369.80","triggered_at":"9:44 am","scan_name":"HMA_crossover","scan_url":"hma-crossover-222","alert_name":"HMA crossover","webhook_url":"https:\/\/pundlik.com\/ChartInk\/hmacross.php"}';

$jsonobj= json_decode($json,true);

$stks  = explode(",",$jsonobj["stocks"]);
$price = explode(",",$jsonobj["trigger_prices"]);
$tm    = explode(",",$jsonobj["triggered_at"]);
$scname = explode(",",$jsonobj["scan_name"]);

$xscrip=$stks[0];
$win1 ="https://kite.zerodha.com/chart/ext/tvc/NSE/";
$win1 .= $xscrip;
$win1 .= '/';
$win1 .= '325121';

$xscrip=$stks[1];
$win2 ="https://kite.zerodha.com/chart/ext/tvc/NSE/";
$win2 .= $xscrip;
$win2 .= '/';
$win2 .= '87297';

?>
    <script type="text/JavaScript"> 
      var url = "<?php echo $win1; ?>";
    window.open(url)
     </script>

    <script type="text/JavaScript"> 
      var url = "<?php echo $win2; ?>";
    window.open(url)
     </script>


What I have tried:

Ideally, the above code should execute and open the windows locally once triggered by the external application. But when the external application calls this code, the window.open() commands do not get executed while any other code is executed, including database updates.
How do I force opening of windows on my client? Alternately, how do I get the code to wait till some PUT input is received from the external site - tried crude methods like a do while loop, etc but they give different errors without the desired results.
Posted
Comments
[no name] 6-Sep-22 6:34am    
window.open(url); is clearly block by the anti-popup system in web browser for lots of reason.
It's about security / no spam campaign.
few ad-block or antivirus do same

 <script type="text/JavaScript">     window.open("<?php echo $win1; ?>");    window.open("<?php echo $win2; ?>"); </script>




to activate script like your Js,
the script tag is between body .... /body
Anantbhushan Pundlik 6-Sep-22 7:02am    
Thanks... two queries : (1) how does it work correctly if I open the code in my browser (2) what is the workaround - how do I get it to work (did not understand your solution... I am a novice at this)
Richard Deeming 6-Sep-22 7:05am    
You don't. If the user has a popup blocker installed and enabled - and almost everyone does these days - then you can only open a new window in response to direct user interaction; for example, when the user clicks an element on your page.

Popping up a new window as soon as your page loads is strictly forbidden. There is no way around that, short of asking your users to disable their popup blocker for your site.
OriginalGriff 6-Sep-22 7:42am    
And the chances of that working are pretty low.
I suspect that if a site asked me to do that I'd never visit it again ... just like the "you've got an adblocker, disable it to see our content" sites. :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900