Skip to main content

Using an Iframe

This document explains how to embed the Mypixia widget into your HTML pages using an iframe. This approach can be useful for sandboxing the widget and isolating it from your main application's JavaScript environment.

Basic HTML Integration with Iframe

To embed the Mypixia widget using an iframe, include the following code snippet in your HTML file:

<!DOCTYPE html>
<html>
<head>
<title>Mypixia Widget Example (Iframe)</title>
</head>
<body>
<iframe
src="https://cdn.mypixia.com/widget.html?apiKey=YOUR_API_KEY_HERE&productName=Premium%20T-Shirt&productCategory=Apparel&mainImage=https://example.com/tshirt.png"
width="100%"
height="600px"
frameborder="0">
</iframe>
</body>
</html>

Explanation:

  • <iframe src="...">: This is the iframe element that will load the Mypixia widget.
  • src: The src attribute points to the URL where the Mypixia widget is hosted.
  • Replace YOUR_API_KEY_HERE with your actual API key.
  • The URL includes parameters for product details: productName, productCategory, and mainImage. Make sure to URL-encode any special characters in these values.
  • width: Sets the width of the iframe.
  • height: Sets the height of the iframe. Ensure it's sufficient to display the entire widget.
  • frameborder: Removes the border around the iframe.

Integration with Backend Technologies

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MypixiaWidgetController {

@Value("${mypixia.api.key:YOUR_API_KEY_HERE}") // Default value if not in properties
private String apiKey;

@Value("${product.name:Premium T-Shirt}")
private String productName;

@Value("${product.category:Apparel}")
private String productCategory;

@Value("${product.mainImage:https://example.com/tshirt.png}")
private String mainImage;

@GetMapping("/mypixia-iframe")
public String mypixiaIframe(Model model) {
model.addAttribute("apiKey", apiKey);
model.addAttribute("productName", productName);
model.addAttribute("productCategory", productCategory);
model.addAttribute("mainImage", mainImage);
return "mypixia-iframe"; // mypixia-iframe.html
}
}

mypixia-iframe.html (Thymeleaf example):

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Mypixia Widget Example (Spring Iframe)</title>
</head>
<body>
<iframe
th:src="@{'https://cdn.mypixia.com/widget.html?apiKey=' + ${apiKey} +
'&productName=' + ${productName} +
'&productCategory=' + ${productCategory} +
'&mainImage=' + ${mainImage}}"
width="100%"
height="600px"
frameborder="0">
</iframe>
</body>
</html>

Explanation:

  • The Spring code uses a @Controller to handle the request and pass the API key and product details to the view.
  • @Value annotations inject the API key and product details from the application properties or use default values if not found.
  • Thymeleaf (or another templating engine) is used to dynamically construct the iframe's src attribute with the injected values.

Security Considerations

  • Never hardcode your API key directly in your HTML or JavaScript code.
  • Use environment variables to store your API key and retrieve it at runtime.
  • For production environments, consider passing the API key from your server to the client.
  • Implement appropriate security measures to protect your API key from unauthorized access.
  • When passing data to the iframe via the URL, ensure that the data is properly URL-encoded to prevent injection vulnerabilities.

By following these guidelines, you can easily integrate the Mypixia widget into your HTML pages using an iframe and leverage its powerful design capabilities while maintaining a secure environment.