вот один, который я не вижу в качестве опции. он похож на использование ajax, но явно отличается.
сначала устанавливает источник скрипта непосредственно в файл PHP.
<script type="text/javascript" src="url_to_your_php_file.php" /></script>
вы даже можете передать переменную обратно в PHP файл, такой как этот пример:
<script type="text/javascript" src="url_to_your_php_file.php?var1=value1" /></script>
, затем в «your_php_file.php»:
<?php
// THIS IS A SIMPLE EXAMPLE
// it demonstrates one method of using the src attribute to link
// to a PHP file which can generate javascripts dynamically
// and share data between PHP and javascript
// you may take this learning example and develop it further
// relying on your own coding skills for validating data
// and avoiding errors, of course
header( 'content-type: text/javascript' );
// if you pass a $_GET variable from the javascript
// you should add code to validate your $_GET variable(s)
// you can add code to query a database
// using $_GET['var1'] or some other criteria
// you can add simple variable assignments
$value = 'some value';
// for the OP's needs (assumes the class object has been defined)
$val = $myService->getValue();
?>
function name() {
// pay attention because you need to use quotes properly
// and account for possible quotes in the variable strings
// to avoid both php and javascript errors
// example assumes $val has been returned as a string
// validate $val as needed using your method of choice
var example1 = '<?php echo '"' . $val . '"'; ?>';
var example2 = '<?php echo '"' . $value . '"'; ?>';
var example3 = '<?php echo '"some other data"'; ?>';
alert( example1 + ' / ' + example2 );
}
<?php
// you may even want to include additional files (.php or .js, etc)
@include 'local_path_to_some_other_js_file.js';
@include 'local_path_to_some_other_php_file.php';
exit;
?>
Вы можете использовать window.open
для создания нового окна - эта функция возвращает ссылку на появившееся окно. Затем вы можете отправить сообщение, используя postmessage
.
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
var w = window.open('https://google.com')
w.postMessage('hello', "*"); // can use target domain instead of *