我想在我的 React.js Web 应用程序中包含两种类型的广告
<script async="async" data-cfasync="false" src="//somewebstite.com/invoke.js"></script>
<div id="container-4foobarbaz"></div>
Run Code Online (Sandbox Code Playgroud)
还有这个广告:
<script type="text/javascript">
atOptions = {
'key' : 'somekey',
'format' : 'iframe',
'height' : 250,
'width' : 300,
'params' : {}
};
document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://www.cdnwebsite.com/invoke.js"></scr' + 'ipt>');
Run Code Online (Sandbox Code Playgroud)
我如何将其包含在我的 React 应用程序中?我尝试了一些方法,但没有一个有效,包括:
const script = document.createElement("script");
script.async = true;
script["data-cfasync"] = true;
script.src = "//somewebstite.com/invoke.js"
this.div.appendChild(script);
Run Code Online (Sandbox Code Playgroud)
渲染中是这样的:
<div id="container-4foobarbaz" ref={el => (this.div = el)} >
</div>
Run Code Online (Sandbox Code Playgroud) 我正在维护一个遗留的javascript应用程序,它的组件分为4个JS文件.
它们是"Default.aspx","set1.aspx","set2.aspx"和"set3.aspx".ASPX页面从属于其各自集合的多个(所有不同的)源文件中写出压缩的JS,并将内容类型标题设置为"text/javascript".
通过添加对第一个集的引用并创建主条目对象来调用该应用程序.
<script src="/app/default.aspx" type="text/javascript"></script>
<script type="text/javascript>
var ax;
// <body onload="OnLoad()">
function OnLoad() {
ax = new MyApp(document.getElementById("axTargetDiv"));
}
</script>
Run Code Online (Sandbox Code Playgroud)
在第一组脚本(default.aspx)的末尾是以下完全代码:
function Script(src) {
document.write('<script src="' + src + '" type="text/javascript"></script>');
}
Script("set1.aspx?v=" + Settings.Version);
Run Code Online (Sandbox Code Playgroud)
它加载第二组脚本(set1.aspx).这在所有主流浏览器(IE6-8 Firefox Safari Opera Chrome)中都没有任何错误.
但是,由于我一直在使用这个脚本安静,我想在很多地方简化函数调用并错误地内联上面的Script函数,从而得到以下代码:
document.write('<script src="set1.aspx?v=' + Settings.Version + '" type="text/javascript"></script>');
Run Code Online (Sandbox Code Playgroud)
在使用测试页面进行测试时,现在会在所有浏览器中抛出以下错误:
MyApp is not defined.
Run Code Online (Sandbox Code Playgroud)
这发生在这一行:ax = new MyApp(...Visual Studio JS调试器和 Firebug报告它.
我已经尝试了在这个问题的前4个答案中的各种方法无济于事.只有将实际的"添加脚本"代码放在一个函数(即document.write('script')行)中,才能使MyApp成功加载:
如果我将该document.write行放在一个函数中,它就可以工作,否则就不行.发生了什么?
拆分和/或转义脚本文本不起作用.
我注意到很多网站使用它,没有关闭标签.
<script type="text/javascript" src="editor.js">
Run Code Online (Sandbox Code Playgroud)
这种风格也是推荐的,但更长:
<script type="text/javascript" src="editor.js"></script>
Run Code Online (Sandbox Code Playgroud)
我可以这样写吗?它有效还是有更好的方法?
<script type="text/javascript" src="editor.js" />
Run Code Online (Sandbox Code Playgroud) 我需要从Joomla本身以外的程序中获取当前登录到Joomla的用户的信息.我从1.5升级到2.5,而我以前所做的不再适用了.
<?php
define( '_VALID_MOS', 1 );
include_once( 'globals.php' );
require_once( 'configuration.php' );
require_once( 'includes/joomla.php' );
$option="test";
$mainframe = new mosMainFrame( $database, $option, '.' );
$mainframe->initSession();
$my = $mainframe->getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
Run Code Online (Sandbox Code Playgroud)
经过一番研究,我想出了这个:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$my =& JFactory::getUser();
$joomla_name = $my->name;
$joomla_email = $my->email; …Run Code Online (Sandbox Code Playgroud)