简体   繁体   English

如何通过网址传递JavaScript变量?

[英]How do I pass a javascript variable through a URL?

I've recently learned 2 simple functions... I'm passing includes via php with links such as: 我最近学习了2个简单的函数...我正在通过php传递包含以下链接的链接:

a href="portfolio.php&content=picture-one" 

I'm using js to cycle between items I have hidden on the page using: 我正在使用js在以下页面之间隐藏的项目之间循环:

function resumeText(text) 
{
    document.getElementById('resume-target').innerHTML =
        document.getElementById(text).innerHTML;
}

Both are working just fine. 两者都很好。 But now I'm trying to have a bit of fun with this and wondered if there is a way to 'merge' the two? 但是现在我试图对此进行一些娱乐,并想知道是否存在一种“融合”两者的方法? I don't want to build the resume page out as a bunch of includes but thought it'd be nice on the site map (or for other links) to be able to jump to one of the 'text' values first instead of the one that loads by default 我不想将简历页面构建为包含一堆内容,但我认为在站点地图(或其他链接)上能够先跳至“文本”值之一而不是跳至该值很不错。默认加载的一个

I don't think I completely understand your question. 我认为我不完全理解您的问题。 If you want a "URL parameter" that's accessible via javascript, check out window.location.hash . 如果您希望通过JavaScript可以访问“ URL参数”,请查看window.location.hash You could then append #section1 , #section2 , etc. to your URL and have your javascript select content accordingly. 然后,您可以将#section1#section2等添加到您的URL中,并让您的JavaScript相应地选择内容。

Choosing a section based on a GET variable 根据GET变量选择一个部分

An easy way to choose a section based on $_GET['content'] is to just have PHP output some javascript. 选择基于$_GET['content']的部分的一种简单方法是仅使PHP输出一些javascript。 In the <head> section of your HTML, you could do this: 在HTML的<head>部分中,您可以执行以下操作:

<script>
var section = '<?php
if($_GET['content'] == "picture-one") {
    echo 'section-1'; // the ID of the section to show
}

else {
    echo 'section-2';
}
?>'
</script>

Then, you can call resumeText(section); 然后,您可以调用resumeText(section); and it will show the section ID that was stored in that variable based on the contents of $_GET['content'] . 并根据$_GET['content']的内容显示存储在该变量中的段ID。

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

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