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 7561673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:13:04+00:00 2026-05-30T13:13:04+00:00

I wanted to pass PHP variables to Javascript without triggering any new http request

  • 0

I wanted to pass PHP variables to Javascript without triggering any new http request (aka: inserting it directly in markup). But I wanted the content as is (without any sanitization that could change my values, even if they where markup itself). Of course I wanted to keep it safe as well.

The best way i’ve came up so far includes json + base64_encode + data uri schemes:

<script type="text/javascript" src="data:text/javascript;base64,<?php echo base64_encode('var thing = '.json_encode($thing)); ?>"></script>

My question is: will this have any side effect? can I safely use this?

  • 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-05-30T13:13:05+00:00Added an answer on May 30, 2026 at 1:13 pm

    I certainly wouldn’t do this. You’re introducing unnecessary compatibility problems (IE). By base64 encoding, you’re bloating the size of your JSON by ~37%.

    <script type="text/javascript">var thing = <?php echo json_encode($thing); ?></script>
    

    Realistically, the only problem you might run in to is if $thing has a ‘</script>‘ in a string somewhere. (It looks like json_encode() actually escapes all forward slashes /, so this isn’t a problem.) HTML parsers will ignore anything else that might look like markup in a <script> block.

    You do have to watch out for text encoding if your page isn’t UTF-8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using plupload to upload files to my server ( http://www.plupload.com/index.php ), however I
just wanted to know how to call php script from running C code on
Say I wanted to pass an array like this: [['Person1', '14'], ['Person2', '26']] To
All, I wanted to know how I could pass a $variable from view, for
I'm writing an IRC bot in PHP, and I wanted to make a s
Im new to php, I dont have good idea how to make things between
I am using php 5.3.6. When I create a new object, the following works:
Up until now, whenever I wanted to pass some raw data to a function
I am using CXF Jax-rs, wanted to pass 2 JSON objects as arguments to
I'm using PHP, I wanted to know if a user lands on a certain

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.