简体   繁体   English

如何将HTML变量值显示为HTML href标记

[英]How to display Javascript Variable value to HTML href tag

i am rewriting my url and category name passing in to url. 我正在重写传递给url的URL和类别名称。 Now i am creating Url Friendly String using JS function is: 现在我正在使用JS函数创建Url Friendly String:

function getUrlFriendlyString(str)
{
   // convert spaces to '-'
   //alert(str);
   str = str.replace(/ /g, "-");
   // Make lowercase
   str = str.toLowerCase();
   // Remove characters that are not alphanumeric or a '-'
   str = str.replace(/[^a-z0-9-]/g, "");
   // Combine multiple dashes (i.e., '---') into one dash '-'.
   str = str.replace(/[-]+/g, "-");
   return str;
}

now on HTML page this JS function is giving a string value in JS like 现在在HTML页面上,此JS函数在JS中给出了字符串值,例如

<? $cat_name= 'Personalized Chocolates / Gift'; ?>
<script>var url=getUrlFriendlyString("<?=$cat_name;?>");</script>

i want to pass var url value in html tag href like that 我想像这样在html标签href中传递var url

<a  href="<?PHP echo SITE_URL;?>/category/<script>document.write(url);</script>/<?=$data_2['cat_id'];?>"

but

<script>document.write(url);</script>

not giving their value and coming url is like that 没有给出他们的价值和即将到来的URL是这样的

http://localhost/ecom/category/<script>document.writeln(url);</script>/105

required url is 所需的网址是

http://localhost/ecom/category/personalized-chocolates-gift/105

any idea how i can show proper url using js string? 任何想法,我怎么能显示正确的URL使用JS字符串?

i can save var url value in PHP variable 我可以将var url值保存在PHP变量中

You can't include an element (such as <script>...</script> ) in the middle of an attribute value. 您不能在属性值的中间包含元素(例如<script>...</script> )。

If you are going to take this approach, then you need to write out the entire anchor element using JavaScript. 如果要采用这种方法,则需要使用JavaScript写出整个锚元素。

An alternative approach would be to set a default value in the HTML, then access the element with DOM in a script that appears after the element is complete. 一种替代方法是在HTML中设置默认值,然后在元素完成后出现的脚本中使用DOM访问元素。

A better approach would be to rewrite the getUrlFriendlyString function in PHP and run it on your server. 更好的方法是用PHP重写getUrlFriendlyString函数,然后在服务器上运行它。

This is working for me 这对我有用

you can take var url value in php variable and use in your href tag 您可以在php变量中使用var url值,并在href标记中使用

<? $cat_name= 'Personalized Chocolates / Gift'; ?>
<script>var url=getUrlFriendlyString("<?=$cat_name;?>");</script>

and

<?php
        $m="<script>document.write(url);</script>";
        echo $m; // output is : personalized-chocolates-gift
   ?>

this will not give you same o/p inside href, will give you o/p your js code because inside $m js code is saved and href not converting js into string o/p 这不会在href内给您相同的o / p,而是会给您o / p您的js代码,因为在$ m内保存了js代码,并且href不会将js转换为字符串o / p

i hope this will work for you :) 我希望这对你有用:)

这可以帮助您使用HTML代码中的JS redirectTo()是JS函数

<a onclick="redirectTo();">click here</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM