Jump to content

Make a page go somewhere specific other than the top when it refreshes


Olegario

Recommended Posts

This brings up my complete novice question:

How make a page go somewhere specific other than the top when it refreshes?

Edited by cyberRobot
Topic extracted from here: https://forums.phpfreaks.com/topic/301689-move-to-top-of-page-after-execution/
Link to comment
Share on other sites

First

  • don't hijack other people's posts
  • don't resurrect old posts

If you are using PHP, what is shown is completely under your control. If you don't want to show the top half of the page, hide it or don't output it.

If you want the whole page to be present but just want to position halfway down then specify the page fragment you want to go to with a "#" in the url.

Link to comment
Share on other sites

Tutorial may be a bit old. With HTML5 named anchor tags not required - just element ids. The sample below uses <h1 id='xxx'> elements. So if you load with, say,

 

http://localhost/sample.html#halfway

 

it will go directly to the halfway element

<!DOCTYPE html> 
<html>
<head>
    <title>Sample</title>
    <style type='text/css'>
        div { height:1500px; background-color: gray; padding: 50px; color: white;}
    </style>
</head>
<body>
    <h1 id='top'>Top</h1>
    <a href='#halfway'>Go to half-way point</a>
    <div>
        Some content
    </div>
    
    <h1 id='halfway'>Half-way</h1>
    <a href='#top'>Go to top of page</a>
    <div>
        Some more content
    </div>
    
</body>
</html>   

 

Link to comment
Share on other sites

12 hours ago, Barand said:

First

  • don't hijack other people's posts
  • don't resurrect old posts

If you are using PHP, what is shown is completely under your control. If you don't want to show the top half of the page, hide it or don't output it.

If you want the whole page to be present but just want to position halfway down then specify the page fragment you want to go to with a "#" in the url.

Sorry—I probably did know better—and thanks for moving the post.

I am using an internal link but it's having no effect. It works if I put the #backhere in the address bar, though.  Here's my code, based on what a colleague gave me.

                        <a id="backhere" name="backhere"></a>
                        <h2>Questions? Need More Information? Contact us at:</h2>


                        <?php if ($_REQUEST['EMail'] != "info") { ?>
                        <FORM ACTION="<?php echo $_SERVER['/example.org/sub/#backhere']; ?>" METHOD="post">
                            <INPUT TYPE="hidden" NAME="EMail" VALUE="info">
                            <INPUT TYPE="submit" value="Show E-mail Address">
                        </FORM>
                        <?php } else { ?>

                        <A HREF="mailto:johndoe@example.org" TITLE="Email John">johndoe@example.org</A>

                        <?php } ?>
 

Link to comment
Share on other sites

Why don't you describe to us what you are trying to do. That FORM ACTION looks well dodgy.

It looks like you want to go to the "backhere" when the form is submitted, which would give you a form to take you to the "backhere" which would give you a form to take you to the "backhere" and so ad infinitum.

Link to comment
Share on other sites

The user clicks a "show email address" button. The same page is redrawn, showing the email address (with mailto: link) instead of the button. But the page focus is at the top of the page and the user has to scroll to the bottom to see the email address they just asked for.

Note I didn't write this script except I added the internal link stuff. I suspect the colleague who gave me the script didn't write it either.

Link to comment
Share on other sites

This should do what you are trying to do.

<div style="height:1500px; background-color: #ffe;">
    Padding to give page more content
</div>

<h2 id="backhere">Questions? Need More Information? Contact us at:</h2>


<?php if ( !isset($_GET['email']) || $_GET['email'] != "info") { ?>
    <form action="#backhere" method="get">
        <input type="hidden" name="email" value="info">
        <input type="submit" value="Show e-mail address">
    </form>
<?php } else { ?>

<a href="mailto:johndoe@example.org" title="Email John">johndoe@example.org</a>

<?php } ?>

However I think using javscript would be a better approach so that everything happens clientside instead of reloading the page.

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type='text/javascript'>
        $().ready( function() {
            
            $("#showemail").click( function() {
                  $(this).css("display","none")
                  $("#email_link").css("display", "inline")
            })
        })
    </script>
    
    <style type='text/css'>
        #email_link {
            display: none;
        }
    </style>
</head>
<body>
    <div style="height:1500px; background-color: #ffe;">
        Padding to give page more content
    </div>

    <h2 id="backhere">Questions? Need More Information? Contact us at:</h2>
    <button id="showemail">Show email address</button>
    <a id="email_link" href="mailto:johndoe@example.org" title="Email John">johndoe@example.org</a>
</body>
</html>

 

Link to comment
Share on other sites

Thank you for the PHP—it works, of course. There is some weirdness in the address bar: www.example.org/sub/?email=info#backhere

The purpose of the PHP is to hide the email address or addresses in the HTML from spambots, until spambots learn to click buttons.

It looks like with the javascript, spambots will see the email address in the HTML.

Link to comment
Share on other sites

The way it was explained to me is that when someone opens the .php file, php processes the file and presents the button but not the email address because of the if/then/else. At this point spambots search the file for email addresses and find none. You and I can press the button to see the address. Kind of like a captcha.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.