Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:48:29+00:00 2026-06-17T22:48:29+00:00

I would like to execute a javascript code within the normal header tags, before

  • 0

I would like to execute a javascript code within the normal header tags, before I redirect the user to another website, but I cannot figure it out how to actually make it work so the code will be executed and the visitor will be redirected.

I want the following:

<html>
 <head>
  //script here
 </head>
 <body>
 </body>
</html>

<?php header ('Location: example.com') ?>

Can someone please tell me how to do it correctly, so it will not return any ‘Headers already sent’ errors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:48:30+00:00Added an answer on June 17, 2026 at 10:48 pm

    If visitors get to this page by following links from your site, you could send additional data to let your server script know that JavaScript is enabled; e.g.

    $('a').each(function() {
        var url = this.href;
    
        if (url.indexOf("?") !== -1)
            this.href = url + '&js=1';
        else
            this.href = url + '?js=1';
    });
    

    This will change all links to include a special parameter that indicates whether JavaScript is enabled or not. If the additional JavaScript code you wish to run can be placed here as well, that would be even better!

    The server script will use something like if (!empty($_GET['js'])) { ... } to either write a piece of JavaScript (old answer 2) or perform a redirect straight away.

    Old answer

    You can send a Refresh header in case JavaScript is disabled.

    <?php
    
        header('Refresh: 2; url=http://www.example.org');
    
    ?>
    <html>
     <head>
      //script here
     </head>
     <body>
     </body>
    </html>
    

    I have to admit that I did not try this, but there’s a good chance it may work

    Old answer 2

    If you’re sure that JavaScript is enabled, though, you could let JavaScript perform the redirect after it’s done; if the url is always the same, you leave out the PHP code and just use a static string:

    <html>
     <head>
      <script>
      //script here
      location = <?php echo json_encode('http://www.example.org'); ?>;
      </script>
     </head>
     <body>
     </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to execute JavaScript code from within a C# assembly and have the
here I would like to execute both javascript and JSP after onclick event <CODE>
I would like to execute code sequentially in an NSOperation. This can be easily
I would like to execute an Ajax request every seconds. My next code work
I would like to execute a frame redirect in C# from my managed module
I would like to execute some program through ssh and redirect its input from
I have a jar file that I would like to execute using PHP, but
I'm looking for a clean way to call/execute/redefine javascript code from a webpage, within
As per Refused to execute a JavaScript script. Source code of script found within
I am trying to execute JavaScript using Winforms & I would like to fetch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.