Ho to store multiline text in a javascript variable;
我正在使用PHP为javascript变量赋值.
请参阅下面的示例代码
<html>
<head>
<title> New Document </title>
<?php
$foo = " this is a
multiline statement";
?>
<script>
bar = '<?php print $foo;?>';
alert(bar);
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我不想丢失任何空白字符.这怎么办?
我正在研究一个项目,它已经完成,但它还需要一个中间化以及它在javascript文件中应用语言常量.
考虑我们包含一个javascript文件,我们有类似的东西
alert("YOU Have VOTED BEFORE");
Run Code Online (Sandbox Code Playgroud)
这样简单,脚本会提醒该文本
但是如果我们需要提醒它的语言常量呢:
alert("<?php echo _VOTED_BEFORE?>");
Run Code Online (Sandbox Code Playgroud)
这将只有在像我的PHP代码的内联回应脚本时才能工作...
但是,我们如何读取并包含外部JavaScript文件的php常量或$ vars?
the_title()在这种情况下,xxxi 返回一些文本Blue & Whiny
我们可以看到的问题是&字符看起来不同
如何打开Blue & Whiny到Blue & Whiny我tryed: ,,htmlspecialchars_decode(the_title()) 并没有什么.html_entity_decode(the_title())htmlspecialchars(the_title())
我想转换&为&
没有太多的代码要分享,我只是这样做:<?php the_title() ?>我得到了Blue & Whiny.如果我使用get_the_title()它不会显示任何东西
有任何想法吗?谢谢
EDIT1.生病分享一些代码:
<script type="text/javascript">
function showShareUI() {
var act = new gigya.services.socialize.UserAction();
act.setUserMessage("Check out this article.");
act.setTitle("Trends on Explore Talent - <?php the_title(); ?>");
act.setDescription("<?php get_the_content(); ?>");
act.setLinkBack("<?php the_permalink(); ?>");
act.addActionLink("Check out this article", "<?php the_permalink(); ?>");
var image = {
src: 'http://xxx.com/wp-content/uploads/2011/05/BOTTOM_BANNER.jpg',
href: …Run Code Online (Sandbox Code Playgroud) 我需要一种方法来存储JavaScript中PHP变量的HTML.
var contents = "{{ $template }}";
Run Code Online (Sandbox Code Playgroud)
然而,这会产生错误.我想这是因为它没有正确转义,所以它弄乱了网络浏览器,因为JS无法正确存储...我已经尝试过Laravel转义
var contents = "{{ e($template) }}";
Run Code Online (Sandbox Code Playgroud)
没有任何成功.
最终目标是: $('#preview-iframe').contents().find('html').html(contents);
我怎么能做到这一点?
在 Laravel 5 刀片模板中,我有一个<script>部分。
在本节中,我需要将一个变量设置为来自另一个刀片模板的字符串。
我试过类似的东西:
<script>
var a = "@include 'sometext.blade.php' ";
</script>
Run Code Online (Sandbox Code Playgroud)
但这显然行不通。
此外,包含的刀片可以包含单引号和双引号,因此需要以某种方式对它们进行转义,否则 Javascript 将无效。
有任何想法吗?
我在PHP中有一个foreach循环:
var circle_<?php echo $x; ?> = '<?php echo $c; ?>';
Run Code Online (Sandbox Code Playgroud)
它在JavaScript中返回:
var circle_0 = '<p class="medium">With us, you can customize more than just a closet.</p>';
var circle_1 = '<p class="medium">We are boutique condo builders who love what we do.</p>';
var circle_2 = '<p class="medium">who says condos can't be spacious?<br/></p>';
Run Code Online (Sandbox Code Playgroud)
如您所见,circle_2在字符串中有一个撇号,因此它会破坏脚本:
未捕获的SyntaxError:意外的标记ILLEGAL
我将使用什么过滤器来解决这个问题(PHP或JavaScript)?
是的我知道这个问题被问了很多,但我对JS很新,我需要在某些JS中使用php变量.我更了解PHP是服务器端执行而JS是客户端,但其他人声称这是有效的.
我有一个名为"test1"的PHP变量,我想记录到JS控制台(例如):
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>';
?>
Run Code Online (Sandbox Code Playgroud)
这样做是打印到JS控制台.不完全是我所希望的.
现在这可能甚至不可行,我可能不得不将变量从页面传递出来并再次使用AJAX,但如果有可用的话,我宁愿有一个快速简便的解决方案!
任何帮助表示赞赏.
我有一个PHP代码变量,我想在我的JavaScript函数中访问它.以下是我的代码.
myfile.php
<?php
$i = 0;
?>
<html>
<head>
<script type="text/javascript">
function SetText() {
//I want to access value of i here
//alert(i);
}
</script>
</head>
<body>
<button type="button" id="mybutton" onclick="SetText()">Click ME</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在JavaScript代码中访问php代码中声明的变量有哪些方法?
我在PHP中生成一个字符串,然后最终将此字符串传递到Javascript警告框中,我的问题是我实际上无法在警报框中添加换行符.
我的代码如下所示
$str = "This is a string\n";
$alert = $str."This is the second line";
if(!empty($alert)){
?>
<script type="text/javascript">
$(document).ready(function() {
alert('<?=$alert?>');
});
</script>
<?php
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误
未确定的字符串文字
如果我从字符串中删除\n,它可以100%工作,但没有换行符
我对Javascript/jQuery和PHP的概念很陌生.我已经使用在Appserv中实现的PHP两周了,从modbus设备获取数据并将其存储在csv文件中.现在我想使用jQplot绘制数据.我正在尝试编写一个简单的程序,首先看看我是否可以在html中一起实现php和javascript代码.这是我用html编写的代码,并使用javascript和php.
<!doctype html>
<html>
<head>
<title>Demo</title>
</head>
<body>
<h2>This is a script</h2>
<script type="text/javascript">
var out = <?php echo "Hello"?>;
//var out = "Hello"
document.write(out);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中运行此代码时(我使用谷歌浏览器与Windows 7)我只得到标题"这是一个脚本".但是,如果我删除PHP代码行,并取消注释下一行
var out = "Hello";
Run Code Online (Sandbox Code Playgroud)
然后代码打印输出"Hello"就像它应该的那样.为什么是这样?