Jump to content

Recommended Posts

Hi, I am using Mouseflow analytics in a phpbb forum.
In my specific application, I need to add their application's javascript code inside php code.
How do I properly add Javascript code inside a php code.
Thanks

This is how I would add it to an html file

    <script type="text/javascript">
        var _mfq = _mfq || [];
        (function () {
        var mf = document.createElement("script"); mf.type = "text/javascript"; mf.async = true;
        mf.src = "//cdn.mouseflow.com/projects/b22343cd9-0049-4443-8326-954444462d6515.js";
        document.getElementsByTagName("head")[0].appendChild(mf);
      })();
    </script>
 

Link to comment
https://forums.phpfreaks.com/topic/293445-adding-javascript-code-to-php-file/
Share on other sites

You can add that same as would in html but before or after the <?php ?> tags

 

It's possible to break in and out of php to html

js script can go here

<?php

//php code

?>

js script can go here

<?php

//php code

?>

js script can go here

 

Best place to put items like this is in a script that always gets included into every page, like your header file.

 

Here is the prosilver template in phpbb 3.1.2

Located in styles/prosilver/template/overall_header.html

 

I added the script within the <head></head> tags

<!DOCTYPE html>
<html dir="{S_CONTENT_DIRECTION}" lang="{S_USER_LANG}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" />
{META}
<title><!-- IF UNREAD_NOTIFICATIONS_COUNT -->({UNREAD_NOTIFICATIONS_COUNT}) <!-- ENDIF --><!-- IF not S_VIEWTOPIC and not S_VIEWFORUM -->{SITENAME} - <!-- ENDIF --><!-- IF S_IN_MCP -->{L_MCP} - <!-- ELSEIF S_IN_UCP -->{L_UCP} - <!-- ENDIF -->{PAGE_TITLE}<!-- IF S_VIEWTOPIC or S_VIEWFORUM --> - {SITENAME}<!-- ENDIF --></title>
<!-- IF S_ENABLE_FEEDS -->
<!-- IF S_ENABLE_FEEDS_OVERALL --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {SITENAME}" href="{U_FEED}" /><!-- ENDIF -->
<!-- IF S_ENABLE_FEEDS_NEWS --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_NEWS}" href="{U_FEED}?mode=news" /><!-- ENDIF -->
<!-- IF S_ENABLE_FEEDS_FORUMS --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_ALL_FORUMS}" href="{U_FEED}?mode=forums" /><!-- ENDIF -->
<!-- IF S_ENABLE_FEEDS_TOPICS --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_TOPICS_NEW}" href="{U_FEED}?mode=topics" /><!-- ENDIF -->
<!-- IF S_ENABLE_FEEDS_TOPICS_ACTIVE --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_TOPICS_ACTIVE}" href="{U_FEED}?mode=topics_active" /><!-- ENDIF -->
<!-- IF S_ENABLE_FEEDS_FORUM and S_FORUM_ID --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FORUM} - {FORUM_NAME}" href="{U_FEED}?f={S_FORUM_ID}" /><!-- ENDIF -->
<!-- IF S_ENABLE_FEEDS_TOPIC and S_TOPIC_ID --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_TOPIC} - {TOPIC_TITLE}" href="{U_FEED}?f={S_FORUM_ID}&t={S_TOPIC_ID}" /><!-- ENDIF -->
<!-- ENDIF --><!-- IF U_CANONICAL -->
<link rel="canonical" href="{U_CANONICAL}" />
<!-- ENDIF --><!--
phpBB style name: prosilver
Based on style:   prosilver (this is the default phpBB3 style)
Original author:  Tom Beddard ( http://www.subBlue.com/ )
Modified by:
--><link href="{T_THEME_PATH}/print.css?assets_version={T_ASSETS_VERSION}" rel="stylesheet" type="text/css" media="print" title="printonly" />
<!-- IF S_ALLOW_CDN --><link href="//fonts.googleapis.com/css?family=Open+Sans:600&subset=latin,cyrillic-ext,latin-ext,cyrillic,greek-ext,greek,vietnamese" rel="stylesheet" type="text/css" media="screen, projection" /><!-- ENDIF -->
<link href="{T_STYLESHEET_LINK}" rel="stylesheet" type="text/css" media="screen, projection" />
<link href="{T_STYLESHEET_LANG_LINK}" rel="stylesheet" type="text/css" media="screen, projection" />
<link href="{T_THEME_PATH}/responsive.css?assets_version={T_ASSETS_VERSION}" rel="stylesheet" type="text/css" media="only screen and (max-width: 700px), only screen and (max-device-width: 700px)" /><!-- IF S_CONTENT_DIRECTION eq 'rtl' -->
<link href="{T_THEME_PATH}/bidi.css?assets_version={T_ASSETS_VERSION}" rel="stylesheet" type="text/css" media="screen, projection" />
<!-- ENDIF --><!-- IF S_PLUPLOAD -->
<link href="{T_THEME_PATH}/plupload.css?assets_version={T_ASSETS_VERSION}" rel="stylesheet" type="text/css" media="screen, projection" />
<!-- ENDIF --><!--[if lte IE 9]>
<link href="{T_THEME_PATH}/tweaks.css?assets_version={T_ASSETS_VERSION}" rel="stylesheet" type="text/css" media="screen, projection" />
<![endif]--><!-- EVENT overall_header_head_append -->{$STYLESHEETS}
<!-- EVENT overall_header_stylesheets_after -->

<script type="text/javascript">
        var _mfq = _mfq || [];
        (function () {
        var mf = document.createElement("script"); mf.type = "text/javascript"; mf.async = true;
        mf.src = "//cdn.mouseflow.com/projects/b22343cd9-0049-4443-8326-954444462d6515.js";
        document.getElementsByTagName("head")[0].appendChild(mf);
      })();
</script>

</head>
<body id="phpbb" class="nojs notouch section-{SCRIPT_NAME} {S_CONTENT_DIRECTION} {BODY_CLASS}"><!-- EVENT overall_header_body_before --><div id="wrap">
<a id="top" class="anchor" accesskey="t"></a>
<div id="page-header">
  <div class="headerbar">
   <div class="inner">   <div id="site-description">
    <a id="logo" class="logo" href="<!-- IF U_SITE_HOME -->{U_SITE_HOME}<!-- ELSE -->{U_INDEX}<!-- ENDIF -->" title="<!-- IF U_SITE_HOME -->{L_SITE_HOME}<!-- ELSE -->{L_INDEX}<!-- ENDIF -->">{SITE_LOGO_IMG}</a>
    <h1>{SITENAME}</h1>
    <p>{SITE_DESCRIPTION}</p>
    <p class="skiplink"><a href="#start_here">{L_SKIP}</a></p>
   </div>   <!-- IF S_DISPLAY_SEARCH and not S_IN_SEARCH -->
   <div id="search-box" class="search-box search-header">
    <form action="{U_SEARCH}" method="get" id="search">
    <fieldset>
     <input name="keywords" id="keywords" type="search" maxlength="128" title="{L_SEARCH_KEYWORDS}" class="inputbox search tiny" size="20" value="{SEARCH_WORDS}" placeholder="{L_SEARCH_MINI}" />
     <button class="button icon-button search-icon" type="submit" title="{L_SEARCH}">{L_SEARCH}</button>
     <a href="{U_SEARCH}" class="button icon-button search-adv-icon" title="{L_SEARCH_ADV}">{L_SEARCH_ADV}</a>
     {S_SEARCH_HIDDEN_FIELDS}
    </fieldset>
    </form>
   </div>
   <!-- ENDIF -->   </div>
  </div>  <!-- INCLUDE navbar_header.html -->
</div><!-- EVENT overall_header_page_body_before --><a id="start_here" class="anchor"></a>
<div id="page-body">
  <!-- IF S_BOARD_DISABLED and S_USER_LOGGED_IN and (U_MCP or U_ACP) -->
  <div id="information" class="rules">
   <div class="inner">
    <strong>{L_INFORMATION}{L_COLON}</strong> {L_BOARD_DISABLED}
   </div>
  </div>
  <!-- ENDIF -->
  <!-- EVENT overall_header_content_before -->

Be careful regarding how much JavaScript you generate with PHP.  It could become a troubleshooting nightmare.  Some believe it should never be done, but I defining a couple of variables is acceptable, and then using your static JavaScript to react to those variables.

Can't you put non-php code in single quotes within the <?php ?> tags? I have used that method to alter php output, eg. color, font size, etc... is javascript different?

 

Example :

<?php
echo '<span class="notification">'.'<font color="red">*</font>'.'&nbsp'."Sentence"."<br>".'<br>';
?>
Edited by greenace92

Wouldn't be much sense trying to get it into php since already is html.

 

You can echo javascript code out providing it's escaped properly.

 

This...

echo '<span class="notification">'.'<font color="red">*</font>'.'&nbsp'."Sentence"."<br>".'<br>';

could easily become... (besides all the unnecessary concatenation, font is deprecated long ago, use style directly or css rules)

echo "<span class='notification' style='color: red;'> * Sentence </span>";

or

echo "<p class='notification'><span style='color: red;'> * </span> Sentence</p>";

Well... it doesn't work

 

Please ignore my useless post hahaha

<?php

echo '<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="JavaScript" type="text/javascript">
<script>
function redirect() {
    window.location.replace("http://www.something.com/");
    }
}
</script>'.'<button onclick="redirect()">Redirect</button>';

?>

Why do you NEED php to output that pure JS that doesn't even contain any PHP? It is totally unnecessary. Just put it in regular HTML in the HEAD of your document.

 

You also have an extra <script> tag within a <script> tag and an extra }

You also aren't using window.location correctly if you want to redirect. What's the replace doing? Nothing.

<head>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

function redirect() {
  window.location = "http://www.something.com/";
}
</script>
</head>
<body>
  <button onclick="redirect()">Redirect</button>
</body>
Edited by CroNiX
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.