简体   繁体   中英

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:

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

I'm using js to cycle between items I have hidden on the page using:

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 . You could then append #section1 , #section2 , etc. to your URL and have your javascript select content accordingly.

Choosing a section based on a GET variable

An easy way to choose a section based on $_GET['content'] is to just have PHP output some javascript. In the <head> section of your HTML, you could do this:

<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); and it will show the section ID that was stored in that variable based on the contents of $_GET['content'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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